walker6o9
asked on
Actionscript 3.0 random movement and text
I'm not sure how to do random movement and textColors for a moving dynamic textbox in ActionScript 3.0. This is what the code looks like in AS 2.0
dx = 0
dy = 0
randomColor = 0xFFFFFF
//randomFont = "Times"
counter = -200
timer = Math.random() *100 + 20
timer2 = (timer*1000)/24
onEnterFrame = function()
{
counter++
if(counter>timer){
timer = Math.random() *100 + 20
counter = 0
dx = Math.random()* 6 - 3
dy = Math.random() * 6 - 3
switchNum = Math.floor(Math.random()*4)
switch(switchNum){
case 1:
randomColor = 0xFFFFFF
break;
case 2:
randomColor = 0x000000
break;
case 3:
randomColor = 0x000000
break;
case 4:
randomColor = 0xFFFFFF
break;
}
}
bouncy._x += dx
bouncy._y += dy
bouncy.body.textColor = randomColor
bouncy.secondBody.textColor = randomColor
}
Arg, forgot; Add "var" before randomColor, randomFont, counter, timer, and timer2 the way it's before dx and dy. Also add a semicolon on line 12.
ASKER
I got the following errors:
1067: Implicit coercion of a value of type int to an unrelated type String.
1067: Implicit coercion of a value of type int to an unrelated type String.
var dx:Number = 0;
var dy:Number = 0;
var randomColor:String = 0xFFFFFF;
//randomFont:String = "Times";
var counter:Number = -200;
var timer:Number = Math.random() * 100 + 20;
var timer2:Number = (timer*1000)/24;
var switchNum:Number;
addEventListener(Event.ENTER_FRAME, enterHandler);
function enterHandler(e:Event) {
counter++;
if(counter>timer){
timer = Math.random() *100 + 20;
counter = 0;
dx = Math.random()* 6 - 3;
dy = Math.random() * 6 - 3;
switchNum = Math.floor(Math.random()*4);
switch(switchNum){
case 1:
randomColor = 0xFFFFFF;
break;
case 2:
randomColor = 0x000000
break;
case 3:
randomColor = 0x000000
break;
case 4:
randomColor = 0xFFFFFF
break;
}
}
var format:TextFormat = new TextFormat();
// format.font = randomFont;
format.color = randomColor;
bouncy._x += dx;
bouncy._y += dy;
bouncy.body.defaultTextFormat = format;
bouncy.secondBody.defaultTextFormat = format;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window