AS3 child issue with if statements
Alright, can someone explain what is wrong with my code below, there is no errors, but it's not doing what I want it to do. I need it to display a movieclip on the screen when a variable called "randint", which is generated by random, is greater than or equal to 0.5. If it's not then it doesn't get displayed. Code:
addEventListener(Event.ENTER_FRAME, char_coll);
function char_coll(ev : Event) : void
{
if(currentFrame==2)
{
if (randint >= 0.5){
var w1:woman1 = new woman1();开发者_如何学Go
randint = Math.random();
if(w1.hitTestObject(stand)){
w1.gotoAndPlay(1);
cash1 = cash1 + 1;
}
}
}
};
randint
is set inside the if statement. This means that randint
always is undefiend, because it has to be >= 0.5
to be set to any value (kind of a catch 22).
This code should work:
addEventListener(Event.ENTER_FRAME, char_coll);
function char_coll(ev : Event) : void
{
if(currentFrame==2)
{
var randint:Number = Math.random();
if (randint >= 0.5){
var w1:woman1 = new woman1();
stage.addChild(w1);
if(w1.hitTestObject(stand)){
w1.gotoAndPlay(1);
cash1 = cash1 + 1;
}
}
}
};
Then of course you have to add w1
to the stage using addChild()
as you can se below var w1:woman1 = new woman1();
hope it helps!
精彩评论