# How do I create random motion for targets?

Page 1 of 1

## 3 Replies - 3049 Views - Last Post: 12 September 2010 - 05:36 AM

### #1 ifsogirl

Reputation: 0
• Posts: 22
• Joined: 15-March 08

# How do I create random motion for targets?

Posted 11 September 2010 - 07:23 AM

Okay, I'm making a flash game; a very basic shooting game. Everything works so far, but I want to take it a step further. I want my 'targets' (there is one that gives you points and one that takes them away) to actually move on the screen. Unfortunately, my teacher never went over this with me. I could really use a tutorial on randomly moving objects, or at least some suggestions on how to get started. This is what I have:

```//ifsogirl
//Shooting Game (GAME210 final)
//Sept. 11, 2010

var degrees:Number;
var speed:Number = 10;
var score:int = 0;

//##############################################
//Woodward (Game Loop)
//##############################################
function gameLoop(e:Event)

{
followMouse();
}

//##############################################
//##############################################
function followMouse():void
{
radians = Math.atan2(mouseY - player_mc.y, mouseX - player_mc.x);
player_mc.rotation = degrees + 90;
}

//##############################################
//Shoot Function
//##############################################
function shoot(e:MouseEvent):void
{
var Bullet:bullet = new bullet();
Bullet.rotation = degrees;
Bullet.x = player_mc.x;
Bullet.y = player_mc.y;
}

//##############################################
//Move Bullet Function
//##############################################
function moveBullet(e:Event):void
{

if (e.target.hitTestObject(target_mc))
{
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
{
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
}

//##############################################
//Score Function
//##############################################
{
score += i;
textyBox_txt.text = String(score);
if(score <= 0)
{
DEATH();
if(i <= 0)
{
score -= i;
}
else
{
score += i;
}
}
}

//##############################################
//Death Function
//##############################################
function DEATH()
{
textyBox_txt.text = "YOU FAIL."
}
```

I have no idea where to even start with this, except I think it would be a new functiona and something that would go in the Game Loop. If it would help, I'll attach the file in another post. Any recommendations would help, please.

Is This A Good Question/Topic? 0

## Replies To: How do I create random motion for targets?

### #2 bonyjoe

Reputation: 178
• Posts: 548
• Joined: 08-September 10

## Re: How do I create random motion for targets?

Posted 11 September 2010 - 08:56 AM

It depends how you want them to move.

If you want them to move from one side of the screen to the other you need to generate a random number for a start position variable and then get them to move in the x direction to the other side of the screen.

If you want them to move randomly you can have specific cases such as move in x direction this much or y direction this much and then have a random number generated to choose which case is used in a loop so they will be constantly moving left right up down etc

### #3 ifsogirl

Reputation: 0
• Posts: 22
• Joined: 15-March 08

## Re: How do I create random motion for targets?

Posted 11 September 2010 - 09:04 AM

Okay, so... Say I just want them to scroll across the y axis back and forth. Is there a known function that would allow this?

### #4 bonyjoe

Reputation: 178
• Posts: 548
• Joined: 08-September 10

## Re: How do I create random motion for targets?

Posted 12 September 2010 - 05:36 AM