Adobe Flash CS6 AS3: KeyUp Handler triggers immediately after KeyDown Handler - actionscript-3

So in my code I was attempting to make it where you had two players, each player had their own key down and key up handlers, clearly labelled in my attached code, and in their I handle the movement, player one has WSAD and player two uses the Left, Right, Up and Down keys, however, when ever I press the relative key down handler, it immediately fires the key up handler straight after, and I do not know why, here is my code, if anyone could help that would be great! :)
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip{
var playerOneLeft:Boolean = new Boolean(false);
var playerOneRight:Boolean = new Boolean(false);
var playerOneUp:Boolean = new Boolean(false);
var playerOneDown:Boolean = new Boolean(false);
var playerTwoLeft:Boolean = new Boolean(false);
var playerTwoRight:Boolean = new Boolean(false);
var playerTwoUp:Boolean = new Boolean(false);
var playerTwoDown:Boolean = new Boolean(false);
var playerSpeed:int = 5;
var gamePage:GamePage;
public function Main(){
gamePage = new GamePage();
addChild(gamePage);
addAllPlayerHandlers();
}
public function playerOneKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = true;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = true;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = true;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = true;
trace("s");
}
}
public function playerOneKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = false;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = false;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = false;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = false;
trace("s");
}
}
public function playerTwoKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = true;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = true;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = true;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = true;
trace("down");
}
}
public function playerTwoKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = false;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = false;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = false;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = false;
trace("down");
}
}
public function playerMovementHandler(event:Event):void{
if(playerOneLeft){
gamePage.playerOne.x -= playerSpeed;
}
else if(playerOneRight){
gamePage.playerOne.x += playerSpeed;
}
else if(playerOneUp){
gamePage.playerOne.y += playerSpeed;
}
else if(playerOneDown){
gamePage.playerOne.y -= playerSpeed;
}
if(playerTwoLeft){
gamePage.playerTwo.x -= playerSpeed;
}
else if(playerTwoRight){
gamePage.playerTwo.x += playerSpeed;
}
else if(playerTwoUp){
gamePage.playerTwo.y += playerSpeed;
}
else if(playerTwoDown){
gamePage.playerTwo.y -= playerSpeed;
}
}
public function playerOneCollision(event:Event):void{
if(gamePage.playerOne.hitTestObject(gamePage.leftWall)){
gamePage.playerOne.x += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.rightWall)){
gamePage.playerOne.x -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.topWall)){
gamePage.playerOne.y -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.bottomWall)){
gamePage.playerOne.y += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.middleWall)){
gamePage.playerOne.x -= playerSpeed;
}
}
public function playerTwoCollision(event:Event):void{
if(gamePage.playerTwo.hitTestObject(gamePage.leftWall)){
gamePage.playerTwo.x += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.rightWall)){
gamePage.playerTwo.x -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.topWall)){
gamePage.playerTwo.y -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.bottomWall)){
gamePage.playerTwo.y += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.middleWall)){
gamePage.playerTwo.x += playerSpeed;
}
}
public function addPlayerMovementHandler(){
stage.addEventListener(Event.ENTER_FRAME,playerMovementHandler);
}
public function addPlayerKeyHandlers(){
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyUpHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyUpHandler);
}
public function addPlayerCollisionHandlers(){
gamePage.playerOne.addEventListener(Event.ENTER_FRAME,playerOneCollision);
gamePage.playerTwo.addEventListener(Event.ENTER_FRAME,playerTwoCollision);
}
public function addAllPlayerHandlers(){
addPlayerKeyHandlers();
addPlayerCollisionHandlers();
addPlayerMovementHandler();
}
}

when ever I press the relative key down handler, it immediately fires the key up handler straight after
This is because you registered them as
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyUpHandler); // this is for type KEY_DOWN, but the handler is "Up"
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyDownHandler); //that looks suspicious, too
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyUpHandler);
The lesson you should learn is that your copy & paste approach to writing code is a bad practice. You should not repeat yourself. All this duplicated code leads to problems like the one at hand.

Related

How to add score when collecting things (Actionscript 3 Adobe Animate)?

How to create an incremented score after he pickup the rubbish? I am very new on this thing and need help on coding.
My code below is on a separated .as file (movieclip).
As you can see, there are codes for when the player hittestobject, the "rubbish" will go away but how to apply a new code for score to that specific function? so the score will be incremented each time he pick up the rubbish.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class mazeClass extends MovieClip {
var mouseUp:Boolean = false;
var mouseDown:Boolean = false;
var mouseLeft:Boolean = false;
var mouseRight:Boolean = false;
var speed:Number = 5;
public function mazeClass() {
// constructor codea
stage.addEventListener(KeyboardEvent.KEY_DOWN, CheckDownKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, CheckUpKeys);
stage.addEventListener(Event.ENTER_FRAME, updatePos);
}
private function CheckDownKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =true;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =true;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =true;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =true;
}
}
private function CheckUpKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =false;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =false;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =false;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =false;
}
}
private function updatePos(e:Event){
if(mouseUp == true){
if(!wall.hitTestPoint(jacob.x,jacob.y-22, true)){
jacob.y -= speed;
}
}
if(mouseDown == true){
if(!wall.hitTestPoint(jacob.x, jacob.y+22, true)){
jacob.y += speed;
}
}
if(mouseLeft == true){
if(!wall.hitTestPoint(jacob.x-22, jacob.y, true)){
jacob.x -= speed;
}
}
if(mouseRight == true){
if(!wall.hitTestPoint(jacob.x+22, jacob.y, true)){
jacob.x += speed;
}
}
if(jacob.hitTestObject(a1)){
a1.x = a1.y = -1000;
_root.score = 0;
_root.score++;
score.text = _root.score;
}
if(jacob.hitTestObject(a2)){
a2.x = a2.y = -1000;
}
if(jacob.hitTestObject(a3)){
a3.x = a3.y = -1000;
}
if(jacob.hitTestObject(a4)){
a4.x = a4.y = -1000;
}
}
}
}

Flash Game Actionscript-3 Output Error #1009

I am making my own game using Actionscript 3 for coding in Flash. The game is about a Runner Character just like Super Mario and Stickyman, who just run, jump, dies.. and so on..
After being very organized by dividing each window of the game in a different Scene, I was still having the problem of executing the piece of code "gotoAndStop()".. That's why I decided to make it much simple by using only one Scene, but each wind of the game in a different frame in the Main Timeline. So I worked on the root, but still getting the same problem that stuck in a white screen!
Anyways, one of the main problem that I am facing here is the Error #1009. Even when I tried to read other topics with the same issue but I didn't find the answer that suites my case.
This is the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HR4_fla::MainTimeline/movePlayer()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Apple/update()
Here is the Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;
stop();
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var score:int = 0;
var lives:int = 3;
player_mc.health = 100;
player_mc.dead = false;
//var TouchRestartBox:Boolean = false;
var playerSpeed:Number = 8;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump: Boolean = false;
//var appleCount:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(event:Event):void
{
scrollRect = new Rectangle(player_mc.x - stage.stageWidth/2, player_mc.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
//PressKey function here
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
//ReleaseKey function here
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
//stage.addEventListener(Event.ENTER_FRAME, GameOver);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
function movePlayer(event:Event):void
{
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x+= playerSpeed;
player_mc.scaleX = 0.59;
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x-= playerSpeed;
player_mc.scaleX = -0.59;
}
if(upKeyIsDown && canJump)
{
player_mc.gotoAndStop(3);
yVelocity = -15;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity = -13;
canDoubleJump = false;
}
yVelocity +=gravity;
if(!floor_mc.hitTestPoint(player_mc.x,player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for(var i:int=0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
for(var j:int=0; j<=2; j++)
{
if(rb.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.x = -1703.35;
player_mc.y = 322.1;
player_mc.scaleX = 0.59;
lives = lives - 1;
}
if(lives == 0)
{
// GameOver();
// remove all the event listeners
// stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
gotoAndStop(124);
//(root as MovieClip).gotoAndStop(124);
}
}
// appleCount_txt.text = "Apples:" + appleCount;
}
/*function GameOver()
{
// lives = 3;
// remove all the event listeners
stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
// player_mc.stop();
gotoAndStop(1); // this has your "dead" screen on it.
}*/
Can anyone help me solving this problem please?
Thanks
There's a listener still getting executed after the frame change. In that handler, an object is referenced that does not exist on that frame and is thus null.
Changing frames only provides visual changes, not necessarily a complete change of state.

Linking multiple objects to one coding name

I am trying to make an rpg style game in Flash AS3. I am trying to get my character to stop when he hits objects such as trees and buildings. Is there a way I can link multiple objects together and make the code say something like 'take object1, object2, and object3 and name it multipleobjects'? I have the code set up so that the character stops at one tree but I'm not positive how/if you can combine objects so that he won't go through multiple at one time. Thank you very very much in advanced!
Objects:
manmc (my character), treer1_MC (the first tree) treer2_MC (second tree) and so on
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
manmc.gotoAndStop ("Stand Front Frame");
var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var manSpeed:Number = 3;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function keyDownHandler (keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = true;
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = true;
}
}
function keyUpHandler (keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
manmc.gotoAndStop("Stand Right Frame");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
manmc.gotoAndStop("Stand Left Frame");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
manmc.gotoAndStop("Stand Back Frame");
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
manmc.gotoAndStop("Stand Front Frame");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed)
{
if(manmc.x < 1050)
{
manmc.x += manSpeed;
}
manmc.gotoAndStop("Walk Right Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("leftHit");
manmc.x -= 3;
}
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("rightHit");
manmc.x += 3;
}
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("downHit");
manmc.y -= 3;
}
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("upHit");
manmc.y += 3;
}
}
}
Use an array to keep track of multiple objects.
Instead of manipulating the single object, you then use a loop to iterate over all the elements in the array which are the single objects.

AS3 error 1084 syntax error expecting rightparen before dot

i am new to this whole as3 thing and i am struggling immensely. I have been sat for the last two days trying to do something i can imaging to be simple to everyone else reading this. I am trying to create a game where i have a skate boarder controlled by the keyboard keys. However when i type this code in i am getting a 1084 error please help before i throw my laptop out the window. Thanks!!
package {
import flash.display.*;
import flash.events.*;enter code here
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
the_skater.addChild:(skater);
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = true;
} else if (event.keyCode == 39) {
moveRight = true;
} else if (event.keyCode == 65) {
moveUp = true;
} else if (event.keyCode == 90) {
moveDown = true;
}
}
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = false;
} else if (event.keyCode == 39) {
moveRight = false;
} else if (event.keyCode == 65) {
moveUp = false;
} else if (event.keyCode == 90) {
moveDown = false;
}
}
function moveskater(event:Event) {
var speed:uint = 20;
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0){
skater.x = 800;
}
}
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800){
skater.x = 0;
}
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0){
skater.y = 0;
}
}
if (moveDown) {
skater.y += speed;
if (skater.y > 0){
skater.y = 0;
}
}
I tried your code but didn't get your error. Your sample throws other errors & problems.
So my advice is these two things..
The correct to construct your code...
package
{
//IMPORTS go here
//Declare your Class
public class skatefate extends MovieClip
{
//VARS go here
//*******************************************************************
//note: later you may also add other VARS inside functions as needed
//(but were not originally put (declared) in this section)
//*******************************************************************
//Declare main function of your Class (must have same name as Class (.as)
public function skatefate()
{
//Constructor code here
//************************************************************************
// Your main program code and related functions (K/board etc) go here and
// will reference your VARS declared above in public Class construction)
//************************************************************************
} //End of (public) Function
} //End of (public) Class
} //End of Package
Just incase you still struggle, this edit of your code shown should compile. From there you can study & learn. Hopefully one more laptop will survive in this cruel world.
package
{
import flash.display.*;
import flash.events.*; //enter code here
//Declare your Class
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
var skater:Sprite = new Sprite(); //hide line if skater exists already (i.e in Library)
var speed:uint = 20;
//Declare main function of your Class
public function skatefate ()
{
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
the_skater.addChild(skater);
addChild(the_skater); //adds to stage
function keyPressedDown (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = true; }
else if (event.keyCode == 39) { moveRight = true; }
else if (event.keyCode == 65) { moveUp = true; }
else if (event.keyCode == 90) { moveDown = true; }
}
function keyPressedUp (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = false; }
else if (event.keyCode == 39) { moveRight = false; }
else if (event.keyCode == 65) { moveUp = false; }
else if (event.keyCode == 90) { moveDown = false; }
}
function moveskater(event:Event)
{
//var speed:uint = 20; //already declared at top
//speed = 20; // later change 'speed' this way by updating number
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0)
{ skater.x = 800; }
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800)
{ skater.x = 0; }
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0)
{ skater.y = 0; }
}
if (moveDown) { skater.y += speed;
if (skater.y > 0)
{ skater.y = 0; }
}
} //close 'moveskater' function
} //End of your (public) Function
} //End of your (public) Class
} //End of Package
Hope it helps. Ask for advice in the comments and don't forget to tick as "correct answer" if it works for you. That's how we say "Thanks" on Stack Overflow.

Null object error; cannot access it - AS3

I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.
You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.
You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player