I’ve been following a tutorial written for Actionscript 2, and have successfully converted it to AS 3, however on the second to last part I’m stuck.
The tutorial here (http://www.cleverpig.com/tutorials/whackapig/whack.htm step 8) has the following piece of code:
if (_currentframe==1) {
// randomly choose whether or not to play
if (random(100)>97) {
// should we tease or popup?
if (random(3)<1) {
this.gotoAndPlay ("popup");
} else {
this.gotoAndPlay (1);
}
}
}
it is meant to add some randomness to the character’s movement. After some googling I created this code in AS 3 hoping it would work.
if (currentFrame==1) {
// randomly choose whether or not to play
if(Math.floor(Math.random()*99)-97) {
// should we tease or popup?
if (Math.floor(Math.random() *3)-1)
) {
this.gotoAndPlay ("popup");
} else {
this.gotoAndPlay (1);
}
}
}
When I run the program with this code the character’s entire animation plays once (down, halfway up, up, hit). It is supposed to only play the first 3 frames, and repeat this.
EDIT:
function random (n:int ) : int {
return Math.floor (Math.random() * n);
}
if (currentFrame==1) {
// randomly choose whether or not to play
if(random(100)): 97 {
// should we tease or popup?
if (random(3)): 1
{
this.gotoAndPlay ("popup");
} else {
this.gotoAndPlay (1);
}
}
}
Symbol ‘hole’, Layer ‘Actionscript’, Frame 1, Line 10 1084: Syntax error: expecting identifier before colon.
Symbol ‘hole’, Layer ‘Actionscript’, Frame 1, Line 10 1008: Attribute is invalid.
Symbol ‘hole’, Layer ‘Actionscript’, Frame 1, Line 12 1084: Syntax error: expecting identifier before colon.
Symbol ‘hole’, Layer ‘Actionscript’, Frame 1, Line 13 1008: Attribute is invalid.
Symbol ‘hole’, Layer ‘Actionscript’, Frame 1, Line 15 1083: Syntax error: else is unexpected.
I’d give a -1 to the writer of the original tutorial, putting a numeric random expression in a a conditional like that.
To determine the outcome of a conditional’s expression, it is always evaluated as a Boolean. If you put a Number/int in there, it will be false only if its value is 0(.0). In all other cases (including negative numbers) it will evaluate to true.
Yes, you can make an easier random() function by following @weltraumpirat’s answer, but I’d rather recommend you take the random() function out of the conditional and wrap it in a chance() method of sorts. For example:
Then calling this method will give you a better x out of y syntax like:
Or your other conditional,
feelingLucky(1/3). Or try a winning streak withfeelingLucky(100/100).Good luck 🙂
P.S. Your recent edit fails because of the colons and numeral :97 after the if statements. They shouldn’t be there.