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

New Topic/Question
Reply



MultiQuote



|