3 Replies - 1412 Views - Last Post: 10 March 2013 - 03:42 AM Rate Topic: -----

#1 NoSoulNinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 13

Flash Actionscript 3 Camera

Posted 09 March 2013 - 11:45 AM

Hi all,
Im making a game in flash but i dont know how to make a camera follow the character(i want the characetr to be always in the center of the screen). Ive looked online but i could really find any good tutorials or answers.

Heres my code:

// Initialize/ imports
//-------------------------------------------------------------------------------------------------
import flash.events.KeyboardEvent;

//Defining the variables
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
var space:Boolean = false;

var coinCounter:int; // coin counter. 

var gravity:Number = 1;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump:Boolean = true;
var canTripleJump:Boolean = true;

//Adding Event listeners
//-------------------------------------------------------------------------------------------------
stage.addEventListener(KeyboardEvent.KEY_UP, reportKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(Event.ENTER_FRAME, EnterFrame);

//KeyDown function;
//-------------------------------------------------------------------------------------------------
function reportKeyDown(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		/*case Keyboard.UP :
			{
				//trace("up");
				moveUp = true;
				break;
			};*/
		case Keyboard.S :
			{
				//trace("down");
				moveDown = true;
				break;
			};
		case Keyboard.A :
			{
				//trace("left");
				moveLeft = true;
				break;
			};
		case Keyboard.D :
			{
				//trace("right");
				moveRight = true;
				break;
			}
		case Keyboard.SPACE :
			{
				space = true;
				break;
			}
	}
};


//KeyUp Function
//-------------------------------------------------------------------------------------------------
function reportKeyUp(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		/*case Keyboard.UP :
			{
				//trace("up");
				moveUp = false;
				break;
			};*/
		case Keyboard.S :
			{
				//trace("down");
				moveDown = false;
				break;
			};
		case Keyboard.A :
			{
				//trace("left");
				moveLeft = false;
				break;
			};
		case Keyboard.D :
			{
				//trace("right");
				moveRight = false;
				break;
			}
		case Keyboard.SPACE :
			{
				space = false;
				break;
			}
	}
};


// Movement controls, rotation, jumping, collisions.
// Enter Frame Function 
//-------------------------------------------------------------------------------------------------
function EnterFrame(event:Event):void
{
	if (moveLeft)
	{
		char.x -=  10;
		char.rotation = 180; // rotates pacman 180 degresss
	}
	
	if (moveRight)
	{
		char.x +=  10;
		char.rotation = 0;
	}
	
	/*if (moveUp)
	{
		char.y -=  10;
		char.rotation = -90;
	}*/
	
	if (moveDown)
	{
		char.y +=  10;
		char.rotation = 90;
	}
	
	// jumping
	if(space && canJump)
	{
		yVelocity = -15;
		canJump = false;
		canDoubleJump = true;
		canTripleJump = true;
		
	}
	
	// double jump
	if(space && canDoubleJump && yVelocity > -2)
	{
		yVelocity = -13;
		canDoubleJump = false;
	}
	
	// Triple Jump
	if(space && canTripleJump && yVelocity > -2)
	{
		yVelocity = -13;
		canTripleJump = false;
	}
	
	yVelocity += gravity;
	
	
	// Collision detection.
	// no collision.
	//---------------------------------------------------------------------------------------------
	if(!floor_mc.hitTestPoint(char.x, char.y, true))
	{
		char.y += yVelocity;
	}
	
	// Speed.
	if(yVelocity > 20)
	{
		yVelocity = 20;
	}
	
	for(var i:int = 0; i < 10; i++)
	{
		// checks for collision
		if(floor_mc.hitTestPoint(char.x, char.y, true))
		{
			char.y--;
			yVelocity = 0;
			canJump = true;
		}
	}
	
	// Coin Counter(text)
	//---------------------------------------------------------------------------------------------
	coinCounter_txt.text = "Coins collected = " + coinCounter;
}



Can anyone give me some advice on what to do and how to do it?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Flash Actionscript 3 Camera

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Flash Actionscript 3 Camera

Posted 09 March 2013 - 11:58 AM

You could achieve this quite easily just by moving the background rather than the character.
Was This Post Helpful? 0
  • +
  • -

#3 NoSoulNinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 13

Re: Flash Actionscript 3 Camera

Posted 09 March 2013 - 12:02 PM

Ya i was thinking about doing something like that but how would i get all of the layers moving with eachother?

I have 6 different layers that would have to move with eachother:
1. terrain
2. Timbar
3. coins
4. forest
5. floor
6. background
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Flash Actionscript 3 Camera

Posted 10 March 2013 - 03:42 AM

You could have a MovieClip or Sprite which would serve as the parent for everything else. Then you can simply add the rest of your layers as children and they will all move together.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1