Link to home
Create AccountLog in
Avatar of walker6o9
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
	
	
}

Open in new window

Avatar of Vega152
Vega152
Flag of United States of America image

Most of it is the same.
var dx:Number = 0;
var dy:Number = 0;
randomColor:Color = 0xFFFFFF;
//randomFont:String = "Times";
counter:Number = -200;
timer:Number = Math.random() * 100 + 20;
timer2:Number =  (timer*1000)/24;
 
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;
}

Open in new window

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.
Avatar of walker6o9
walker6o9

ASKER

I got the following errors:
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;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vega152
Vega152
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer