Saturday, June 23, 2012

[Action Script] Array collision problem... June,2012

Hey. So I've been having a lot of trouble trying to get a character in my platformer to detect collisions with an array properly. I've been scouring the internet for a couple days now for a solution but nothing I've seen seems to work properly.

Basically I just want the normal landing on tiles and not falling through them thing. But he lands just fine on the last tile in the tiles array (box3) but falls through when I try to walk him onto others.

Here's the code:

ActionScript Code: var yspeed = 0;var gravity = 0;var accel = 0.5;var maxvel = 4;var speed = 6;var tiles:Array = new Array();tiles[0] = box1;tiles[1] = box2;tiles[2] = box3;var playr = buddy;buddy.addEventListener(Event.ENTER_FRAME, collision);function collision(Event){    for (var i:int = 0; i < tiles.length; i++)    {        if (playr.hitTestObject(MovieClip(tiles[i])))        {            landed = true;        } else {landed = false}    }}stage.addEventListener(Event.ENTER_FRAME, grav);function grav(Event){    if (landed == true)    {        accel = 0;        maxvel = 0;    }    if (landed == false)    {        maxvel = 4;        accel = 0.5;        buddy.y = yspeed +=  gravity;        gravity +=  accel;        if (yspeed == maxvel)        {            yspeed = 0;            buddy.y = yspeed +=  maxvel;        }    }}
(Buddy is the character and the boxes are the tiles that I want him to land on.)
Also, if I'm going about this completely wrong, can someone suggest a better way to do this?
Array collision problem...

Related Post



0 comments: