Copy link to clipboard
Copied
The code checks for a collision and then switches between the six frame depending of the actual frame, if the actual frame is "noDamage" it goes to the next frame, ect.
public function onTick(timerEvent:TimerEvent):void
{
gameClock.addToValue(25);
if(Math.random()<0.01)
{
var randomY:Number = Math.floor(Math.random() * 300) + 50;
var newEnemy:Enemy = new Enemy(400,randomY);
army.push(newEnemy);
addChild(newEnemy);
gameScore.addToValue(10);
}
blade.x = mouseX;
blade.y = mouseY;
var gadjetHasBeenHit:Boolean = false;
for each(var enemy:Enemy in army)
{
if(PixelPerfectCollisionDetection.isColliding(jail,enemy,this,true))
{
var desiredFrame:String = "noDamage";
var desiredFrame1:String = "damage_1";
var desiredFrame2:String = "damage_2";
var desiredFrame3:String = "damage_3";
var desiredFrame4:String = "damage_4";
var desiredFrame5:String = "damage_5";
switch(jail.currentLabel)
{
case "noDamage":
jail.gotoAndStop(desiredFrame1);
break;
case "damage_1":
jail.gotoAndStop(desiredFrame2);
break;
case "damage_2":
jail.gotoAndStop(desiredFrame3);
break;
case "damage_3":
jail.gotoAndStop(desiredFrame4);
break;
case "damage_4":
jail.gotoAndStop(desiredFrame5);
break;
}
trace("Enemy collided with the jail");
}break;
}
Copy link to clipboard
Copied
What problem are you having? Have you used the trace() function to try to solve whatever it is?
Copy link to clipboard
Copied
the switch statement is conducting stright to the last frame, if you collide with another object and if the actual frame label is "noDamage" the you gotoAndPlay "desiredFrame" which is the next frame, if the actual frame object's label is "desiredFrame" then you play "desiredFrame2", ect, but it doesnt work like that, it is playing all the frames at once.
Copy link to clipboard
Copied
that break statement (on the line after your trace) needs to be in the if-statement brackets so you can exit that for-loop.
Copy link to clipboard
Copied
It didnt work Kglad
Copy link to clipboard
Copied
Try something more like:
var hasCollided:Boolean;
for each(var enemy:Enemy in army) {
if(PixelPerfectCollisionDetection.isColliding(jail,enemy,this,true)) {
hasCollided = true;
break;
}
}
if (hasCollided) {
var label = getNextLabel(jail);
if (label!='') {
jail.goToAndStop(label);
} else {
//you were at the last label
}
}
....
protected function getNextLabel(mc:MovieClip):String {
for (var i:int; i<mc.currentFrameLabels.length; i++) {
var frameLabel:FrameLabel = mc.currentFrameLabels;
if (frameLabel = mc.currentFrameLabel && i < mc.currentFrameLabels.length - 1) {
return FrameLabel(mc.currentLabels[i+1]).name;
}
}
return '';
}
This way you can add as may frame labels as you want without having to change your logic. Note that it might be a good idea to move the logic for finding and going to the next label into Jail, so that you could then just do
jail.nextLabel();
Copy link to clipboard
Copied
it also looks like those goto's are in a timer loop. if so, that could be problematic.
in any case, copy and paste your corrected onTick function.
Copy link to clipboard
Copied
Thanks a lot you guys!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now