Object Barriers in AS3? - actionscript-3

I am a beginner at coding but I am trying to make a flash rpg type game. I have a barrier around my game and a walking person (manmc). I have been watching videos/reading articles on how to make object barriers but I can not seem to get the coding right without getting errors. I am trying to get my character to stop when he hits a tree. The tree is coded as (treer1_MC). I only tried to code it on the down press but it does nothing and it causes the character to not be able to walk back up.
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");
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
}
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
}
}
This is the coding a tutorial said to use. Any suggestions?
if (manmc.hitTest(treer1_MC))
trace("leftHit");
manmc._x += 3;

It is just because the declaration of the collision condition is outside of your "downPress" condition:
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
}
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
indentation problems :), the right way should be:
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
}
I hope it helps ;)
Edit:
manmc.hitTest(treer1_MC)
should be:
manmc.hitTestObject(treer1_MC)
Because the function: hitTest() is a AS2 function, on AS3 whe have: hitTestPoint() and hitTestObject() ;)

Related

HitTestObjekt in ActionScribt 3.0 doesn´t work but I don´t get any Error Messages

I´m trying to make a little game in ActionScribt 3.0 for a school projekt in a kind of Pixel RPG style. I wanted to use Arrays and HitTestObjekt to create some hitboxes for the many objects in the game but it just doesn´t work and since I don´t get any error Messages or even anything in the output I just don´t know what is wrong with the code.
The most confusing thing is, that I made several test room to test different functions with hitTestObjekt and it all worked perfectly fine. But once I took this code over to the main map and changed the objects in the Arrays accordingly it just stopped working.
Here is the code I made for it so far, without the stuff for NPCs or enemyies.
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
this.luke_MC.gotoAndStop("Stand Front Frame");
//luke Variablen
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 attackButtonPress:Boolean = new Boolean(false);
var lukeAlive:Boolean = new Boolean(true);
var lukeSpeed: Number = 10;
var lukeSwordPower:Number = 10;
var lukeDirection:String = "front";
var cantGoThrough: Array = new Array();
//EventListener für Tastatur und Enter_Frame
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
stage.addEventListener(Event.ENTER_FRAME, GameLoop);
loadArray();
function loadArray():void {
cantGoThrough[0] = background_MC.HitboxZaun;
}
//------------Tastatur Key Down--------------
function keyDownListener(keyEvent: KeyboardEvent): void {
if (keyEvent.keyCode == Keyboard.RIGHT) {
rightPressed = true;
lukeDirection = "right";
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftPressed = true;
lukeDirection = "left";
} else if (keyEvent.keyCode == Keyboard.UP) {
upPressed = true;
lukeDirection = "back";
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downPressed = true;
lukeDirection = "front";
}
else if (keyEvent.keyCode == Keyboard.SPACE){
attackButtonPress = true;
}
}
//------------Tastatur Key Up------------
function keyUpListener(keyEvent: KeyboardEvent): void {
if (keyEvent.keyCode == Keyboard.RIGHT) {
rightPressed = false;
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftPressed = false;
} else if (keyEvent.keyCode == Keyboard.UP) {
upPressed = false;
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downPressed = false;
}else if (keyEvent.keyCode == Keyboard.SPACE){
attackButtonPress = false;
}
}
/* alte Verion von Key UP ohne Angriff
function keyUpListener(keyEvent: KeyboardEvent): void {
if (keyEvent.keyCode == Keyboard.RIGHT) {
rightPressed = false;
this.luke_MC.gotoAndStop("Stand Right Frame");
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftPressed = false;
this.luke_MC.gotoAndStop("Stand Left Frame");
} else if (keyEvent.keyCode == Keyboard.UP) {
upPressed = false;
this.luke_MC.gotoAndStop("Stand Front Frame");
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downPressed = false;
this.luke_MC.gotoAndStop("Stand Back Frame");
}else if (keyEvent.keyCode == Keyboard.SPACE){
attackButtonPress = false;
}
}
*/
//---------------------------------------------------------------
//------------Game Loop, Bewegung des Spielers und der Map--------
//---------------------------------------------------------------
function GameLoop(loopEvent: Event): void {
//------lukes und Maps Bewegung------------
if (rightPressed) {
if (this.luke_MC.x < 800) {
this.luke_MC.x += lukeSpeed;
} else if (background_MC.x > -9464) {
this.background_MC.x -= lukeSpeed;
}
this.luke_MC.gotoAndStop("Walk Right Frame");
} else if (leftPressed) {
if (this.luke_MC.x > 200) {
this.luke_MC.x -= lukeSpeed;
} else if (background_MC.x < -4850) {
this.background_MC.x += lukeSpeed;
}
this.luke_MC.gotoAndStop("Walk Left Frame");
} else if (upPressed) {
if (this.luke_MC.y > 200) {
this.luke_MC.y -= lukeSpeed;
} else if (background_MC.y < -2469) {
this.background_MC.y += lukeSpeed;
}
this.luke_MC.gotoAndStop("Walk Back Frame");
} else if (downPressed) {
if (this.luke_MC.y < 500) {
this.luke_MC.y += lukeSpeed;
} else if (background_MC.y > -4773) {
this.background_MC.y -= lukeSpeed;
}
this.luke_MC.gotoAndStop("Walk Front Frame");
}
else if (attackButtonPress){
//---------------Angriff Frames und Gegner bekommt Schaden vom Schwert---------------------
switch (lukeDirection){
case "left":
this.luke_MC.gotoAndStop("Attack Left Frame");
if(luke_MC.swordLeft_MC.hitTestObject(background_MC.enemy_MC)){
background_MC.enemy_MC.x -= 10; //Gegner wird vom Schwert zurückgestoßen
enemyHealth -= lukeSwordPower;
}
break;
case "right":
this.luke_MC.gotoAndStop("Attack Right Frame");
if(luke_MC.swordRight_MC.hitTestObject(background_MC.enemy_MC)){
background_MC.enemy_MC.x += 10; //Gegner wird vom Schwert zurückgestoßen
enemyHealth -= lukeSwordPower;
}
break;
case "front":
this.luke_MC.gotoAndStop("Attack Front Frame");
if(luke_MC.swordFront_MC.hitTestObject(background_MC.enemy_MC)){
background_MC.enemy_MC.y -= 10; //Gegner wird vom Schwert zurückgestoßen
enemyHealth -= lukeSwordPower;
}
break;
case "back":
this.luke_MC.gotoAndStop("Attack Back Frame");
if(luke_MC.swordBack_MC.hitTestObject(background_MC.enemy_MC)){
background_MC.enemy_MC.y += 10; //Gegner wird vom Schwert zurückgestoßen
enemyHealth -= lukeSwordPower;
}
break;
}
}
else
{
switch(lukeDirection){
case "left":
this.luke_MC.gotoAndStop("Stand Left Frame");
break;
case "right":
this.luke_MC.gotoAndStop("Stand Right Frame");
break;
case "front":
this.luke_MC.gotoAndStop("Stand Front Frame");
break;
case "back":
this.luke_MC.gotoAndStop("Stand Back Frame");
break;
}
}
//---------Hittest für Objecte etc.--------------
for (var i : int = 0; i< cantGoThrough.length; i++){
if (luke_MC.footBox_MC.hitTestObject(cantGoThrough[i])) {
switch (lukeDirection) {
case "left":
lukeSpeed = 0;
luke_MC.x += 10;
break;
case "right":
lukeSpeed = 0;
luke_MC.x -= 10;
break;
case "back":
lukeSpeed = 0;
luke_MC.y += 10;
break;
case "front":
lukeSpeed = 0;
luke_MC.y -= 10;
break;
}
} else {
lukeSpeed = 10;
}
}
This is the first time I´m asking a question here, I hope I did it right.

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;
}
}
}
}

Character jumping while moving right and left AS3

I am making a platform game where the main character moves around and jumps.
I want the character to jump left and right separately. Maybe using two keys at the same time And land on top of the floor. My characters movie clip symbol is Naruto and my floor movie clip symbol is floor.
My project file can be found here: Naruto Game
In order to do this I have a main movie-clip with all the other movie-clips inside such as "jump right" and "jump left".
What I'm having a problem with, is when THE USER IS MOVING RIGHT I WANT THE CHARACTER TO FACE RIGHT WHEN JUMPING (and the same with the left).
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
naruto.gotoAndStop("stance");
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 narutoSpeed: Number = 10;
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;
naruto.gotoAndStop("standright")
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftPressed = false;
naruto.gotoAndStop("standleft")
} else if (keyEvent.keyCode == Keyboard.UP) {
upPressed = false;
naruto.gotoAndStop("stance")
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downPressed = false;
naruto.gotoAndStop("stance")
}
}
function gameLoop(loopEvent: Event): void {
if (rightPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
} else if (leftPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
} else if (upPressed) {
naruto.gotoAndStop("jumpright");
}
}
I owe so much to the person that can solve this I have been trying to solve this for a week! Thank you very much!
To solve this, you need change your else statements into additionalif statements (so instead of being one or the other, you can potentially get both actions - jump & right).
Here is a code example:
function gameLoop(loopEvent: Event): void {
//If the right key is pressed, and the left key is NOT pressed.
if (rightPressed && !leftPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
}
if(leftPressed && !rightPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
}
if (upPressed) {
//if up is pressed, and right is pressed
if(rightPressed && !leftPressed) naruto.gotoAndStop("jumpright");
if(leftPressed && !rightPressed) naruto.gotoAndStop("jumpleft");
}
if(leftPressed && rightPressed && !upPressed){
naruto.gotoAndStop("stance"); //if left & right are pressed, they negate each other and you play stance
}
}
With this code, if the right key is pressed, naruto will move right. If up is also pressed, then in addition to moving right, it will switch from the right state to the jumpright 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.

Adobe Flash CS6 AS3: KeyUp Handler triggers immediately after KeyDown Handler

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.