We help IT Professionals succeed at work.

Actionscript 3.0 random movement and text

2,209 Views
Last Modified: 2013-11-11
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

Comment
Watch Question

Commented:
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

Commented:
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.

Author

Commented:
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

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.