How do I create random motion for targets?

Shooting games need moving targets.

Page 1 of 1

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

#1 ifsogirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(MouseEvent.CLICK, shoot);
				   			   
var radians:Number;
var degrees:Number;
var speed:Number = 10;
var score:int = 0;

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

{
	followMouse();
}

//##############################################
//Follow Mouse Function
//##############################################
function followMouse():void
{
	radians = Math.atan2(mouseY - player_mc.y, mouseX - player_mc.x);
	degrees = radians / Math.PI*180;
	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;
	addChild(Bullet);
	Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}

//##############################################
//Move Bullet Function
//##############################################
function moveBullet(e:Event):void
{
	var rads:Number=e.target.rotation*Math.PI/180;
	e.currentTarget.x += Math.cos(rads)*speed;
	e.currentTarget.y += Math.sin(rads)*speed;
	
	if (e.target.hitTestObject(target_mc))
	{
		UpdateScore(1);
		e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
		removeChild(MovieClip(e.target));
	}
	if (e.target.hitTestObject(badStuff_mc))
	{
		UpdateScore(-1);
		e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
		removeChild(MovieClip(e.target));
	}
}

//##############################################
//Score Function
//##############################################
function UpdateScore(i:int)
{
	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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 ifsogirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: How do I create random motion for targets?

Posted 12 September 2010 - 05:36 AM

Try this
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1