0
votes

I'm quite new to AS3, and I'm trying to make a game that has a sort of RPG-like movement, I've been using PixelPerfectCollisionDetection by Troy Gilbert and I'm using that to detect collisions between the character and a movie clip of a collision map, it works quite well, but I don't know how to actually add the collisions in, I've been trying to do the thing where when you hit the collision area, you move the opposite direction back, but it looks dodgy and can make the character go through the wall if you hit a different key, so I'm trying to find a way to make better collisions. The whole movement is based on moving the whole map with the collision map, the charSpeed represents how fast the map moves, and if the collision map hits the character, it detects the collision, the collision is detected from a different class file which dispatches an event if a collision has been made, this class recieves it and turns a boolean to true, it also dispatches an event for no collision so it turns it false. Here is the code for the movement and collision:

                public function runControlTime(timeHandlerEvents:TimeHandlerEvents):void
            {
                    if (charUp == true)
                    {
                            if (collisionDetected)
                            {
                                    y -= charSpeed;
                            }
                            else
                            {
                                    y += charSpeed;
                            }

                    }
                    if (charDown == true)
                    {
                            if (collisionDetected)
                            {
                                    y += charSpeed;
                            }
                            else
                            {
                                    y -= charSpeed;
                            }
                    }
                    if (charLeft == true)
                    {
                            if (collisionDetected)
                            {
                                    x += charSpeed;
                            }
                            else
                            {
                                    x -= charSpeed;
                            }
                    }
                    if (charRight == true)
                    {
                            if (collisionDetected)
                            {
                                    x -= charSpeed;
                            }
                            else
                            {
                                    x += charSpeed;
                            }
                    }
            }
2

2 Answers

0
votes

Assuming that charSpeed is applied to the character in a game loop. You would typically want to reverse the player's direction after they hit the wall.

if (collisionDetected)
{
    charSpeed *= -1;
    x += charSpeed;
}
0
votes

PixelPerfectCollisionDetection is a memory cost way.. you can use another way..

create an array inside your character named var edgPoints:Array = new Array, create an empty movie clip and write the following action line in the first frame parent.edgePoints.push(this), then place this movie clip inside your character and cope/past it around the character edges where you want collision to be detected..

in order for the following code to work you will need to assign the increment speed value to a variable called var velocity:Number=0 and name the movie clip you want the character to collide with when touched as obstacle

then inside the character place this code:

var edgePoints:Array=new Array();

var velocity:Number=0
var velocity_value:Number=5 //the speed by which the charachter will move

//just some parameters no need to panic

var force:Number=0;
var force_angle:Number=0;
var tourq:Number=0;
var tourq_angle:Number=0;



//listeners for testing only

stage.addEventListener(KeyboardEvent.KEY_DOWN,kd);
stage.addEventListener(KeyboardEvent.KEY_UP,ku);

function kd(e:KeyboardEvent) {
      if (e.keyCode==Keyboard.RIGHT) {
        velocity=velocity_value;
      }
      if (e.keyCode==Keyboard.LEFT) {
        velocity=- velocity_value;
      }
}
function ku(e:KeyboardEvent) {
      velocity=0;
}

this.addEventListener(Event.ENTER_FRAME,ef);
function ef(e:Event) {

x+=velocity

//this is physics and will not affect your main motion and will cause the repel action
//once this movie clip collides with obstacles

x+=(force)*Math.cos(force_angle*Math.PI/180);
y+=(force)*Math.sin(force_angle*Math.PI/180);
rotation+=tourq/10;
force-=force/4;
tourq-=tourq/4;

for (var i=0; i<edgePoints.length; i++) {
   var point:Point=this.localToGlobal(new Point(edgePoints[i].x,edgePoints[i].y));
   if (root.obstacle.hitTestPoint(point.x,point.y,true)) {
        force+=Math.abs(velocity);
        force_angle=180+Math.atan2(point.y-y,point.x-x)*180/Math.PI;
        tourq_angle=90-(force_angle-180-rotation);
        tourq=0.1*force*Math.sin(tourq_angle*Math.PI/180)*Math.sqrt((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y))*Math.cos(tourq_angle*Math.PI/180);
        velocity=-velocity/2;
    }
}
}

So Good Luck, remeber to create the movie clip obstacle on stage, and the empty movie clip with the action parent.edgePoints.push(this) which then you will place copies of it inside your character movie clip.. have fun to develop the code..