The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 Viewshey guys just having some trouble and would love some help :)
my problem is enemy collision testing. When i spawn an enemy its y position falls until it touches the ground and then moves left and right which works fine
but when i spawn two the second ones y position works fine but the first is displaced and its y position is within the ground sprite.
heres the code I'm using:
for(var g2:int = 0; g2 < grounds.numChildren; g2 ++){
for(var eg:int = 0; eg < enemyArray.length; eg++){
var enemyHitGround:DisplayObject = grounds.getChildAt(g2);
if(enemyArray[eg].hitTestObject(enemyHit Ground)){
enemyArray[eg].y = enemyHitGround.y;
}else{
enemyArray[eg].y += 2;
}
}
}
I'm sure its something to do with the loops amount of iterations due to the enemyArrays length or something
are you using this in an onEnterFrame function?
if so, this not an optimal code to use.
you always loop each frame and it's CPU intensive
try putting onEnterFrame on each movieclip spawned
mc.onEnterFrame=function(){
}
then you reference the movieclip as "this"
mc.onEnterFrame=function(){
this._y++;
// etc code
} He's using AS3 ("grounds.numChildren").
So, idk if this'll fix the problem or not. But I guess its worth a shot. Right now you loop through each enemy inside each loop of the ground.
So check ground 1, ok he hit so put him on ground1.y . But he didn't hit ground2 (checked afterwards - that's the important piece), so he gets moved down 2 pixels. Maybe that's the displacement you're seeing?
So here's some different code:
for(var eg:int = 0; eg < enemyArray.length; eg++)
{
var yVel:int = 0;
for(var g2:int = 0;g2<grounds.numChildren;g2++)
{
var enemyHitGround:DisplayObject = grounds.getChildAt(g2);
if(enemyArray[eg].hitTestObject(enemyHit Ground))
{
yVel = 0;
enemyArray[eg].y = enemyHitGround.y;
break;
}
else
{
yVel += 2;
}
}
enemyArray[eg].y += yVel;
}
I'm writing this in a cafeteria - so I can't really test it out or anything. Hope it works though!
MSN/Email-> kevin.stubbs@live.com -Need artists!
The 9th Legion
(Looking for somebody to create the site for $$$).
thanks AdmittingZero it worked perfectly, your a life saver :) in return I will look at your submissions and vote generously