PDA

View Full Version : Stepmania / DDR



jns.johansson
January 25th, 2008, 04:26 PM
Hi! Some months ago I used some example from a book, recreated it and created a Stepmania-replica.

It should'nt be that hard, and it isn't since I managed it in AS2, can't in AS3 though (I'm remaking it in AS3 to learn..).



clip.addEventListener(Event.ENTER_FRAME, initiate);

function initiate(e:Event):void {
var currentTime:int = getTimer();
var elapsedTime:int = currentTime - startTime;
if(elapsedTime >= pauseTime) {
var whatStep = routine[stepCount];
var step:Step = new Step();
step.y = 400;
step.name = "step" + count;
step.gotoAndStop(whatStep);
clip.addChild(step);
if(stepCount >= routine.length) {
stepCount = 0;
}
stepCount++;
count++;
startTime = getTimer();
step.addEventListener(Event.ENTER_FRAME, checkStep);
}
}

function checkStep(e:Event) {
var target = e.target;
target.y = target.y - 4;
if(target.y <= 200) {
clip.removeChild(target);
}
}


First I check my "clip" (Empty MovieClip put on Stage) with an ENTER_FRAME. I have a pause-time on 1000 so every 1000 It creates a new arrow. So far so good.

Then my problem comes, I want to send the arrow upwards so I set for the "step" and ENTER_FRAME aswell. This also works well but I want to remove the clip after it passes a certain value. I did the removeChild but get's the error "The supplied DisplayObject must be a child of the caller.".

I kinda understand the error but can't figure out how the logic behind the game really should be set up.

Any pointer would be great, or direct comments. :)

jns.johansson
January 25th, 2008, 06:35 PM
function myFunction3(e:Event) {
var target = e.target;
target.y = target.y - 5;
if(target.y < 0) {
var parent = target.parent;
parent.removeChild(target);
target.removeEventListener(e.type, myFunction3);
}
}


Now this is being desperate..

As I thought it was the eventListener which when the caller got removed traced my error. So when I remove the caller I also remove the eventListener. This can't be the right way. If someone want to check the code it would be awesome, I write pretty clean code on one frame as all other AS3-scripters so it should be pretty easy for the brainiacs to see my probs.

My workaround does work but I cant imagine it being correct.

jns.johansson
January 25th, 2008, 09:06 PM
so intense, I really can't move away from the computer when I'm stuck on something. Ok, I made some progress. Found out that I'd better understand the logic before I try something.

Now picture this.

on Stage is Symbol A with the name "Father". "Father" has a child, symbol B, called "Son".

if I on stage type:


Father.Son.addEventListener(Event.ENTER_FRAME, myFunction);

function myFunction(e:Event) {
var target = e.target;
trace(target.parent.name); // outputs "Father"
}


Now I will doubleClick on Symbol A and type my code in symbol B:



Son.addEventListener(Event.ENTER_FRAME, myFunction);

function myFunction(e:Event) {
var target = e.target;
trace(target.parent.name); // outputs What i want it to output in Symbol A
}


There's more to it, I add childs and such but it all happens at once, there shouldnt be any difference. What I want to do now works, in a matter of speaking.

However, I dont like putting my code at 2 different frames. Is this a complete nono or is it acceptible sometimes?