I’m making a random generated floor, but when a movieclip walks on it , it seems to behave like just a huge block of floor , the MovieClip walks above it, instead of following the image,here’s the code:
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
//variables
var h:Number = 360;
var floor:MovieClip=new MovieClip;
var guy:MovieClip=new MovieClip;
var gravity=10;
guy.graphics.beginFill(000000);
guy.graphics.drawRect(100,100,15,25);
guy.graphics.endFill();
addChild(guy);//guy creation
//floor generation
for (var i:int=0; i<600; i++)
{
if (h==360)
{
if (Math.random() <= 0.75)
{
floor.graphics.beginFill(000000);
floor.graphics.drawRect(i,h,40,40);
floor.graphics.endFill();
addChild(floor);
i += 39;
}
else
{
i += 39;
}
if (i>550)
{
h = 320;
i = 0;
}
}
if (h==320)
{
if (Math.random() <= 0.75)
{
floor.graphics.beginFill(000000);
floor.graphics.drawRect(i,h,40,40);
floor.graphics.endFill();
addChild(floor);
i += 39;
}
else
{
i += 39;
}
}
}
//listeners doesnt matter
addEventListener(Event.ENTER_FRAME,misc);
addEventListener(KeyboardEvent.KEY_UP,checkup);
addEventListener(KeyboardEvent.KEY_DOWN,checkdown);
//functions
var leftarrow:Boolean=false;
var rightarrow:Boolean=false;
function checkdown(a:KeyboardEvent)
{
if(a.keyCode==37)leftarrow=true;
if(a.keyCode==39)rightarrow=true;
}
function checkup(a:KeyboardEvent)
{
if(a.keyCode==37)leftarrow=false;
if(a.keyCode==39)rightarrow=false;
}
function misc(a:Event)
{
guy.y+=gravity;
if(guy.hitTestObject(floor))
{
gravity=0;
}else{gravity=10;}
if(leftarrow)guy.x-=5;
if(rightarrow)guy.x+=5;
}
Thanks in advance.
The function
hitTestObjectevaluates the bounding boxes of twoDisplayObjectinstances. Your character glides across the holes and pits in your floor because the bounding box encapsulates those holes and pits.Detecting collisions on a concave shape is difficult. You might have an easier time constructing your floor out of several convex shapes (instead of a single concave shape) and using
hitTestObjecton each of them.