-
random movment, rotation and speed with an array
OK I have a rock movieclip that is in an array and a timed function that places an instance of the rock on the stage every 5 seconds. So evey 5 seconds a rock is placed onto the stage. What I want is when a rock is placed onto the stage it rotates either clockwise or anticlockwise. So every 5 seconds a rock appears and it will rotate either clockwise or anitclockwise. I also want the rocks to rotate at a random speed. So every time a rock appears its rotation speed will be different.
I also need the rocks to move in a random direction. So every time a rock appears it will travel in a different direction to the previous rock.
Can anyone tell me how I would do this?
Cheers,
James
-
This will get you started...it doesn't account for wrapping the edges of the screen, and when you remove a rock it's critical that you remove it from the array, the stage, and you remove the enterFrame event listener.
PHP Code:
// an array to hold all the different rocks
var rocks:Array = new Array();
// set up a timer to fire every 5 seconds
var rockTimer:Timer = new Timer(5000);
// link timer-fire to call createNewRock();
rockTimer.addEventListener(TimerEvent.TIMER, createNewRock);
// turn the timer on
rockTimer.start();
function createNewRock(e:TimerEvent = null):void{
// create a new instance of Rock (from the library)
var newRock:Rock = new Rock();
// add it to the array and stage
rocks.push(newRock);
addChild(newRock);
// starting position and rotation
newRock.x = Math.random() * stage.stageWidth;
newRock.y = Math.random() * stage.stageHeight;
newRock.rotation = Math.random() * 360;
// starting speed
newRock.speed = Math.random() * 10;
// call updateRock every frame
newRock.addEventListener(Event.ENTER_FRAME, updateRock);
}
function updateRock(e:Event):void{
// get the Rock's speed and rotation
var speed:Number = e.target.speed;
var rotation:Number = e.target.rotation * (Math.PI / 180);
// add the speed to it's existing position
e.target.x += Math.cos(rotation) * speed;
e.target.y += Math.sin(rotation) * speed;
}
-
I already have an array and a timer I just need to know how to get the rocks to rotate either clockwise or anitclockwise and rotate at a random speed.
Here is the code I am using
PHP Code:
stop();
//creat a new array for the Rocks
var RockArray:Array = new Array();
var startTime:int = int(timer);
var RockTimeGap:int = 5;
var timeElapsed:int = 0;
var i:int;
//create the Rock movieclip
var newRock:Rock = new Rock();
//add the Rock to the stage and place it at a random x and y position
addChild(newRock);
newRock.x = Math.random() * stage.stageWidth;
newRock.y = Math.random() * stage.stageHeight;
newRock.scaleX = Math.random();
newRock.scaleY = Math.random();
//add a new Rock to the end of the array
RockArray.push(newRock);
//create the Rock timer
var RockTimer:Timer = new Timer(5000);
var timer:Timer = new Timer(1000);
RockTimer.addEventListener(TimerEvent.TIMER, onTimer);
timer.addEventListener(TimerEvent.TIMER, timedFunction);
//start the Rock timer the timer
RockTimer.start();
timer.start();
function timedFunction(eventArgs:TimerEvent)
{
//display the score in the timer textbox
score_txt.text =(" Score: "+( timer.currentCount + 1));
}
//create a new Rock movieclip
function onTimer(evt:TimerEvent):void{
var newRock:Rock = new Rock();
//add the Rock to the stage and place it at a random x and y position
addChild(newRock);
newRock.x = Math.random() * stage.stageWidth;
newRock.y = Math.random() * stage.stageHeight;
//make the Rocks scale random
newRock.scaleX = Math.random();
newRock.scaleY = Math.random();
//add a new Rock to the end of the array and add to the Rock count
RockArray.push(newRock);
}
//event listener for the Rock array
RockArray[i].addEventListener(Event.ENTER_FRAME,handleRocks);
function handleRocks(event:Event):void {
for (var i:int = 0; i< RockArray.length; i++) {
RockArray[i].rotation += 15;
}
}
And can it be done without using Math.PI and cos and sin. Couldnt I just make a Math.randon number?
Cheers,
James
-
Also with your code the rocks dont rotate. I want them to rotate either clockwise or anticlockwise. Can you just fix up my code and put in the code needed to get it to work. And can oyu do it without Math.PI and cos and sin and e.target because I dont know what they are and I need ot understand the code.
thanks
Last edited by Gathanderoth; 05-10-2008 at 02:16 PM.
-
Ok for the random movement cant I just have a random number for the x and y that is from say 10 to -10 and then tell it to move the rock along the x and y by that random number.
Also could I do this for the random direction and speed as well. create a random number between say 5 and -5 and then tell it ro to rotate by that random number.
thanks
Last edited by Gathanderoth; 05-10-2008 at 09:39 PM.
-
I'm not sure if this thread is still active but I've looked it over and I don't know why it had to be so complicated for such a simple task.
Here's what I would of done.
I'm going to use the term Target_mc for what ever u need to apply it too.
first things first.
ecstablish x and y movement variables
and a rotation speed variable.
var Rotor:Number; //rotation speed;
var Xmove:Number; // x movement speed
var Ymove:Number; // y movement speed
//now you can either use the Math.random or just plain random() don't matter //to me.
Target_mc.Rotor=random(20)-9;
//this will make it choose a number from -9 to 9
Target_mc.Xmove=random(20)-9;
Target_mc.Ymove=random(20)-9;
Target_mc._x+=Xmove; // moves it randomly at a different speed from either // right or left
Target_mc._y+=Ymove; // same thing but up and down
Target_mc._rotation+=Rotor; //same thing but with rotation either counter //or clockwise
/* You may want to include a if for each variable stating if they land on 0 set to whatever
*/
and BAM! problem solve. now was that so hard
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|