bitmap hit testing error - actionscript-3

I'm trying to get my character to bounce back when it hits an object in order to not allow it to touch the objects. My problem is when its touching 2 objects, it gets stuck and moves backwards because of my else statement. Is there any way I can change my code to not allow my character to touch the objects in the first place.
My code:
o1 is the instance name of the objects all contained in a movie clip and p1 is my charcter.
import flash.events.Event;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var hit:Boolean = false;
p1.addEventListener(Event.ENTER_FRAME, moveChar);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
var redRect:Rectangle = o1.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(o1);
var blueRect:Rectangle = p1.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(p1);
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
if(redClipBmpData.hitTest(new Point(o1.x, o1.y),
255,
blueClipBmpData,
new Point(p1.x, p1.y),
255
))
{
o1.filters = [new GlowFilter()];
hit = true;
}
else
{
o1.filters = [];
hit = false;
}
}
function moveChar(event:Event):void{
if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_down");
if(!hit)
p1.y += 5;
else
p1.y -= 10;
}
if(upKeyDown && !downKeyDown && !rightKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_up");
if(!hit)
p1.y -= 5;
else
p1.y += 10;
}
if(rightKeyDown && !upKeyDown && !downKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_right");
if(!hit)
p1.x += 5;
else
p1.x -= 10;
}
if(leftKeyDown && !upKeyDown && !rightKeyDown && !downKeyDown)
{
p1.gotoAndStop("walk_left");
if(!hit)
p1.x -= 5;
else
p1.x += 10;
}
}
function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown = true;
}
if(event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 83){
downKeyDown = true;
}
if(event.keyCode == 65){
leftKeyDown = true;
}
}
function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown = false;
p1.gotoAndStop("still_up");
}
if(event.keyCode == 68){
rightKeyDown = false;
p1.gotoAndStop("still_right");
}
if(event.keyCode == 65){
leftKeyDown = false;
p1.gotoAndStop("still_left");
}
if(event.keyCode == 83){
downKeyDown = false;
p1.gotoAndStop("still_down");
}
}
Thanks in advance
EDIT:
I changed it to what you said , but now my character doesn't want to move at all :<
CODE:
import flash.events.Event;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var hit:Boolean = false;
var redSpeed:Point = new Point();
p1.addEventListener(Event.ENTER_FRAME, moveChar);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
var redRect:Rectangle = o1.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(o1);
var blueRect:Rectangle = p1.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(p1);
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
if(redClipBmpData.hitTest(new Point(o1.x + redSpeed.x, o1.y + redSpeed.y),
255,
blueClipBmpData,
new Point(p1.x, p1.y),
255))
{
o1.filters = [new GlowFilter()];
hit = true;
}
else
{
o1.filters = [];
hit = false;
}
}
function moveChar(event:Event):void{
if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_down");
if(!hit)
{
redSpeed.y = 0;
}
else
{
redSpeed.y = 5;
p1.y += 5;
}
}
if(upKeyDown && !downKeyDown && !rightKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_up");
if(!hit)
{
redSpeed.y = 0;
}
else
{
redSpeed.y = -5;
p1.y -= 5;
}
}
if(rightKeyDown && !upKeyDown && !downKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_right");
if(!hit)
{
redSpeed.x = 0;
}
else
{
redSpeed.x = 5;
p1.x += 5;
}
}
if(leftKeyDown && !upKeyDown && !rightKeyDown && !downKeyDown)
{
p1.gotoAndStop("walk_left");
if(!hit)
{
redSpeed.x = 0;
}
else
{
redSpeed.x = -5;
p1.x -= 5;
}
}
}
function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown = true;
}
if(event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 83){
downKeyDown = true;
}
if(event.keyCode == 65){
leftKeyDown = true;
}
}
function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown = false;
p1.gotoAndStop("still_up");
}
if(event.keyCode == 68){
rightKeyDown = false;
p1.gotoAndStop("still_right");
}
if(event.keyCode == 65){
leftKeyDown = false;
p1.gotoAndStop("still_left");
}
if(event.keyCode == 83){
downKeyDown = false;
p1.gotoAndStop("still_down");
}
}
Is this what you meant ?

I think what you will want to do is do a hittest based on the anticipated locations of the objects. In order to do that, though, you would want assign speed values, instead of moving the objects on moveChar().
private var redSpeed:Point = new Point();
//in moveChat:
if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
{
p1.gotoAndStop("walk_down");
if(!hit)
redSpeed.y = 5;
else
redSpeed.y = 0;
}
// in enterFrame
if(redClipBmpData.hitTest(new Point(o1.x + redSpeed.x, o1.y + redSpeed.y),
255,
blueClipBmpData,
new Point(p1.x, p1.y),
255)) {.....
Then you can just move the object by its speed.

Related

*RPG-Engine Troubleshoot (AS3): "Multiple Levels" code snippet returns a "null" HitTestObject

*Hello!
I'm working on an open-source game engine scripted in AS3
and I am attempting to have the MC advance to another area/level...
*How it Works/The Idea...
When the MC obtains a key, it is used to turn kameHouseLocked to kameHouseOpen.
When the kameHouse is set to open the player can hold the "up arrow" towards the
door to advance. I have two ways to achieve this, either within a movieclip or by
calling whatever second layers are needed layers to the stage. The way my code is
currently set up, it is unable to reference the kameHouse which is needed to trigger
the event. The logic worked well enough for the key, should I make sure whatever is
being referenced in encased within a movieclip? Do I need an independent snippet
entirely to handle the collisions? Any help would really be appreciated,
I will kindly credit you for any effort/involvement, thank you in advance!
*Output spits this out?
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at pokemongameexperiment/keyDownHandler()
*The Code in Question...
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
hide_text.stop();
stop();
//backenvironment.kamiHouse.stop();
var keyCodeToListenForDown = '40'; //downarrow key
var keyCodeToListenForUp = '38'; //uparrow key
var keyCodeToListenForLeft = '37'; //leftarrow key
var keyCodeToListenForRight = '39'; //rightarrow key
var keySinglePress:Boolean = false;
var keyDoublePress:Boolean = false;
var timeToWaitForDoublePress:Number = 300;
var waitingForDoublePress:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
var pp:Boolean=true;
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3
var RF:Array =
["roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
var kameKeyCollected:Boolean = false;
var kameHouseOpen:Boolean = false;
var currentLevel:int = 1;
//kameHouse(2).prevBtn.addEventListener(MouseEvent.CLICK, prevSection);
addEventListener(Event.ENTER_FRAME, onRoshi);
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
//kameHouse(2).nextBtn.addEventListener(MouseEvent.CLICK, nextSection);
//function prevSection(event:MouseEvent):void
//{
//var thisLabel:String = pages.currentLabel;
//var thisLabelNum:String = thisLabel.replace("sct", "");
//var curNumber:Number = Number(thisLabelNum);
//var prevNum:Number = curNumber - 1;
//pages.gotoAndStop("sct" + prevNum);
//}
function handleKeyDown(keyEvent:KeyboardEvent)
{
if (keyEvent.keyCode == keyCodeToListenForDown)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForUp)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForLeft)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForRight)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
}
//hide_text.addEventListener(MouseEvent.CLICK, stopMovie);
function ppState(event:MouseEvent){
if(pp){
stop();
hide_text.gotoAndStop(3);
//hide_text.gotoAndStop('stopText');
pp = false;
}else{
//hide_text.addEventListener(MouseEvent.CLICK, playMovie);
play();
hide_text.gotoAndStop(1);
//hide_text.gotoAndStop('playText');
pp = true;
}
}
hide_text.addEventListener(MouseEvent.CLICK, ppState);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
function nextLevel():void
{
currentLevel++;
trace("Next Level: " + currentLevel);
if(currentLevel == 2)
{
gotoLevel2();
}
}
function gotoLevel2():void
{
//gotoAndStop(2);
//frontenvironment2.gotoAndStop;
backenvironment.gotoAndStop(2);
frontenvironment.gotoAndStop(2);
gotoAndStop(frontenvironment2);
//scrollx = 0;
//scrolly = 0;
//backenvironment.gotoAndStop
//backenvironment.gotoAndStop
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed * 1.05;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed * 1.05;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed * 1.05;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
if (keyDoublePress && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += (mcSpeed * 1.05);
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
//add more walking functionality here if needed (ex: multi-directional).
}
//Roshi's_Brain:
function onRoshi(e:Event):void
{
if(mc.hitTestObject(backenvironment.Roshi))
{
myText.text = "Master Roshi: R- Red? Oh dear... this is bad. I've barely introduced myself. I'm Master Roshi, headmaster of the Turtle School of Martial Arts!";
}
else
{
myText.text = "Master Roshi: Red... You have to find Goku. My back, it's delicate!";
roshiTimer ++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
while (backenvironment.Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
backenvironment.Roshi.gotoAndStop(RF[roshiFacing]);
}
backenvironment.Roshi.x += RX[roshiFacing] * roshiSpeed;
backenvironment.Roshi.y += RY[roshiFacing] * roshiSpeed;
}
if(kameKeyCollected == false)
{ // if we still haven't collected the key
if(mc.hitTestObject(backenvironment.kameKey))
{ // and if the player collides with the key
backenvironment.kameKey.visible = false; // hide the key from view
kameKeyCollected = true; // set our Boolean to true
}
}
if(kameHouseOpen == false)
{ // if the door hasn't been opened yet
if(kameKeyCollected == true)
{ // and if the player has already collected the key
if(mc.hitTestObject(backenvironment.kameHouse))
{ // check if the door and the player are touching
// if all of these conditions are met...
backenvironment.kameHouse.gotoAndStop(2); // ...switch the door's image to its 2nd frame
kameHouseOpen = true; // ...set the variable to true
}
}
}
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
if(kameHouseOpen && mc.hitTestObject(backenvironment.kameHouse.kameHouseLocked))
{
nextLevel();
}
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
Revised Code
///////////////////////////////////////////////////////////////////////////
///Dev Notes: Poke Ball Z (Ver. 0.3.8)//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
hide_text.stop();
stop();
//backenvironment.kamiHouse.stop();
var keyCodeToListenForDown = '40'; //downarrow key
var keyCodeToListenForUp = '38'; //uparrow key
var keyCodeToListenForLeft = '37'; //leftarrow key
var keyCodeToListenForRight = '39'; //rightarrow key
var keySinglePress:Boolean = false;
var keyDoublePress:Boolean = false;
var timeToWaitForDoublePress:Number = 300;
var waitingForDoublePress:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
var pp:Boolean=true;
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3
var RF:Array =
["roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
var kameKeyCollected:Boolean = false;
var kameHouseOpen:Boolean = false;
var currentLevel:int = 1;
//kameHouse(2).prevBtn.addEventListener(MouseEvent.CLICK, prevSection);
addEventListener(Event.ENTER_FRAME, onRoshi);
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
//kameHouse(2).nextBtn.addEventListener(MouseEvent.CLICK, nextSection);
//function prevSection(event:MouseEvent):void
//{
//var thisLabel:String = pages.currentLabel;
//var thisLabelNum:String = thisLabel.replace("sct", "");
//var curNumber:Number = Number(thisLabelNum);
//var prevNum:Number = curNumber - 1;
//pages.gotoAndStop("sct" + prevNum);
//}
function handleKeyDown(keyEvent:KeyboardEvent)
{
if (keyEvent.keyCode == keyCodeToListenForDown)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForUp)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForLeft)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
if (keyEvent.keyCode == keyCodeToListenForRight)
{
keySinglePress = true;
if (waitingForDoublePress){
keyDoublePress = true;}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress){
trace('You logged a double press!')}
else if (keySinglePress) {
trace ('You logged a single press!');
}
}
//hide_text.addEventListener(MouseEvent.CLICK, stopMovie);
function ppState(event:MouseEvent){
if(pp){
stop();
hide_text.gotoAndStop(3);
//hide_text.gotoAndStop('stopText');
pp = false;
}else{
//hide_text.addEventListener(MouseEvent.CLICK, playMovie);
play();
hide_text.gotoAndStop(1);
//hide_text.gotoAndStop('playText');
pp = true;
}
}
hide_text.addEventListener(MouseEvent.CLICK, ppState);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
function nextLevel():void
{
currentLevel++;
trace("Next Level: " + currentLevel);
if(currentLevel == 2)
{
gotoLevel2();
}
}
function gotoLevel2():void
{
//gotoAndStop(2);
//frontenvironment2.gotoAndStop;
backenvironment.gotoAndStop(2);
backenvironment.kameHouseOpen.gotoAndStop(1);
frontenvironment.gotoAndStop(2);
gotoAndStop(frontenvironment2);
//scrollx = 0;
//scrolly = 0;
//backenvironment.gotoAndStop
//backenvironment.gotoAndStop
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
if(kameHouseOpen && mc.hitTestObject(backenvironment.kameHouse))
{
nextLevel();
}
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed * 1.05;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed * 1.05;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(keyDoublePress && mc.currentLabel =="walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed * 1.05;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
if (keyDoublePress && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += (mcSpeed * 1.05);
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
//add more walking functionality here if needed (ex: multi-directional).
}
//Roshi's_Brain:
function onRoshi(e:Event):void
{
if(mc.hitTestObject(backenvironment.Roshi))
{
myText.text = "Master Roshi: R- Red? Oh dear... this is bad. I've barely introduced myself. I'm Master Roshi, headmaster of the Turtle School of Martial Arts!";
}
else
{
myText.text = "Master Roshi: Red... You have to find Goku. My back, it's delicate!";
roshiTimer ++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
while (backenvironment.Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
backenvironment.Roshi.gotoAndStop(RF[roshiFacing]);
}
backenvironment.Roshi.x += RX[roshiFacing] * roshiSpeed;
backenvironment.Roshi.y += RY[roshiFacing] * roshiSpeed;
}
if(kameKeyCollected == false)
{ // if we still haven't collected the key
if(mc.hitTestObject(backenvironment.kameKey))
{ // and if the player collides with the key
backenvironment.kameKey.visible = false; // hide the key from view
kameKeyCollected = true; // set our Boolean to true
}
}
if(kameHouseOpen == false)
{ // if the door hasn't been opened yet
if(kameKeyCollected == true)
{ // and if the player has already collected the key
if(mc.hitTestObject(backenvironment.kameHouse))
{ // check if the door and the player are touching
// if all of these conditions are met...
backenvironment.kameHouseOpen.gotoAndStop(1); // ...switch the door's image to its 2nd frame
kameHouseOpen = true; // ...set the variable to true
}
}
}
}
//function nextSection(event:MouseEvent):void
//{
//var thisLabel:String = currentLabel; //gets current frame label as string
//var thisLabelNum:String = thisLabel.replace("sct", ""); //cuts the leading letters off of the number
//var curNumber:Number = Number(thisLabelNum); //converts that string numbers to a real number
//if (curNumber < 5)
//{
//var nextNum:Number = curNumber + 1;
//gotoAndStop("scl" + nextNum);
//}
//}
New Error Code
You logged a single press!
Next Level: 2
ArgumentError: Error #2109: Frame label null not found in scene Scene 1.
at flash.display::MovieClip/gotoAndStop()
at pokemongameexperiment/gotoLevel2()
at pokemongameexperiment/nextLevel()
at pokemongameexperiment/keyDownHandler()
You logged a single press!
You logged a single press!
You logged a single press!
You logged a single press!
Next Level: 3
You logged a single press! //etc.
*Newgrounds Link to test the game as is...
https://www.newgrounds.com/dump/item/49a43c79e613ec32e547dc1ae8851978

First question! NPC walk-cycle randomizer is working as intended (boundaries do need tweaking), but not playing the nested walking animations? (AS3)

Pretty much, the walking animations won't work with the switch statement I think? That's what it looks like, but the randomizer itself works fine. What I wanted to do was rewrite the working code for the mc, but replace the key inputs with the cases (random walking directions chosen) from the switch statement and plug that rewritten code in separately into the NPC's brain (roshi).
The way I have the code set up is the NPC "roshi" is a movieclip on the stage. Inside are individual movieclips each with frames for each walking animation but they won't play (ex. roshiRightStand, roshiRightWalk, etc). It always gets stuck on the chosen frame, and never actually enters the movieclip no matter how I mess with the code, brackets or booleans. Maybe I'm not declaring or nesting something right or at all? The animations are frames within a movieclip nested within another movieclip and the names seem to match up in the properties? Not quite sure how to declare it, but at some point I believe I may have had the whole thing working and messed it up. Left my old leftover code if it could help? Any input would be much appreciated! Learning fairly quick. :)
*The Code w/ mistakes (slashed out): https://textuploader.com/108de
*The Code cleaned up and tidy: https://textuploader.com/108lw
*Game Upload (reflects the code as is; playable on your browser w/ plugin): https://www.newgrounds.com/dump/item/e06224a5f9fd5645ce5a4604173f8bbd?fbclid=IwAR3HJdXMXEqxUN5TH2xaDvV82QBDmI0ewnVej1EQJFkZLb3RYuEK0dvMz74
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:Number = 0; //random roshi-cycle duration between 0-25
var roshiDuration:Number = Math.random() * 25;
var roshiFacing:Number = Math.floor(Math.random() * 4); //random # bwt 0-3 (ex. 4 is rounded down to 3)
var roshiSpeed:Number = 3; //roshi's walk speed
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
function gameLoop(loopEvent:Event):void
{
//MC's Movement Controls
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650) //right world borderwall
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130) //left world borderwall
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200) //og 200
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10) //top world borderwall
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485) //og 568 LESS MOVES MC UP CAMERA
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577) //bottom world borderwall og-577
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
if(roshiTimer < roshiDuration)
{
switch(roshiFacing) //x=horozontal y=vertical +=right/up -=left/down
{
case 0 :
roshi.gotoAndStop("roshiUpWalk");
//roshi.addEventListener(Event.ENTER_FRAME)
roshi.y -= roshiSpeed;
break;
case 1 :
roshi.gotoAndStop("roshiDownWalk");
roshi.y += roshiSpeed;
break;
case 2 :
roshi.gotoAndStop("roshiLeftWalk");
roshi.x -= roshiSpeed;
break;
case 3 :
roshi.gotoAndStop("roshiRightWalk");
roshi.x += roshiSpeed;
break;
}
roshiTimer ++;
}
if(roshiTimer > roshiDuration)
{
roshiDuration = Math.random() * 25; //25
roshiFacing = Math.floor(Math.random() * 4); //4
roshiTimer = 0; //greater than 0
}
}
~Solved by user Organis.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3
addEventListener(Event.ENTER_FRAME, onRoshi);
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
}
var RF:Array =
[
"roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",
];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
function onRoshi(e:Event):void
{
roshiTimer ++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
while (Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
Roshi.gotoAndStop(RF[roshiFacing]);
}
Roshi.x += RX[roshiFacing] * roshiSpeed;
Roshi.y += RY[roshiFacing] * roshiSpeed;
}
Well, let's try anyway. Put this into separate project along with Roshi graphics. The main difference between your code and this that I don't trigger Roshi.gotoAndStop every frame, just once upon Roshi changing directions.
// Integers, because you don't have to round them.
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3;
addEventListener(Event.ENTER_FRAME, onRoshi);
// Let's do this instead of switch.
var RF:Array = [
"roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",
];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
function onRoshi(e:Event):void
{
roshiTimer++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
// Let's make Roshi ALWAYS change direction
// without occasionally proceeding to the same one.
while (Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
Roshi.gotoAndStop(RF[roshiFacing]);
}
Roshi.x += RX[roshiFacing] * roshiSpeed;
Roshi.y += RY[roshiFacing] * roshiSpeed;
}

Cannot access a property or method of a null object reference. -enterframe.Trying to get palayer out of maze

i'm making a simple game and i'm still learning as3 i want to hit test object on the stage , player with box if hittestobject gotoframe or scene all the objects are on the stage with corect instancename, maintimeline as3 below everything works fine but i get the null error.
it is a maze game.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.ui.Keyboard;
var rightArrow:Boolean = false;
var leftArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var speed:int = 20;
stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
function stage_onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = true;
if(event.keyCode == Keyboard.LEFT) leftArrow = true;
if(event.keyCode == Keyboard.UP) upArrow = true;
if(event.keyCode == Keyboard.DOWN) downArrow = true;
}
function stage_onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = false;
if(event.keyCode == Keyboard.LEFT) leftArrow = false;
if(event.keyCode == Keyboard.UP) upArrow = false;
if(event.keyCode == Keyboard.DOWN) downArrow = false;
}
function stage_onEnterFrame(event:Event):void {
var rect:Rectangle = player.getBounds(this);
var i:int = 0;
var xBump:int = 0;
var yBump:int = 0;
if(rightArrow) {
xBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.right + i, player.y, true)) {
xBump = i - 1;
break;
}
}
}
if(player.hitTestObject(box))
{
// Go to next scene
nextFrame();
}
if(leftArrow) {
xBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.left - i, player.y, true)) {
xBump = -i + 1;
break;
}
}
}
if(upArrow) {
yBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.top - i, true)) {
yBump = -i + 1;
break;
}
}
}
if(downArrow) {
yBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.bottom + i, true)) {
yBump = i - 1;
break;
}
}
}
player.x += xBump;
player.y += yBump;
}

Access of undefined property keyUpHandler

Scene 1, Layer 'script', Frame 1, Line 39
1120: Access of undefined property keyUpHandler.
Ok, so I'm following this tutorial http://as3gametuts.com/2012/04/05/platformer-8/
^ Just to have a source and not claim the work myself
I have created
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
and later in the code i have:
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
My errormessage is at the top, any thoughts on why im getting this? Thanks for reading :P
My entire code:
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
var scrollX:Number = 0;
var scrollY:Number = 500;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var speedConstant:Number = 4;
var frictionConstant:Number = 0.9;
var gravityConstant:Number = 1.8;
var jumpConstant:Number = -35;
var maxSpeedConstant:Number = 18;
var doubleJumpReady:Boolean = false;
var upReleasedInAir:Boolean = false;
var carrotCollected:Boolean = false;
//var carrotGathered.visible = false; //HER
var snowmanHappy:Boolean = false;
var openDoor:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
//trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
//trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
//trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
//trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1;
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1;
}
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed = 0;
}
if(upPressed){
ySpeed = jumpConstant;
}
//DOBBELTHOPP..
if(upReleasedInAir == true){
upReleasedInAir = false;
}
if(doubleJumpReady == false){
doubleJumpReady = true;
}
} else {
ySpeed += gravityConstant;
//DOBBELTHOPP
if(upPressed == false && upReleasedInAir == false){
upReleasedInAir = true;
}
if(doubleJumpReady && upReleasedInAir){
if(upPressed){
doubleJumpReady = false;
ySpeed = jumpConstant; //set the y speed to the jump constant
}
}
}
if(xSpeed > maxSpeedConstant){ //moving right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){ //moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= frictionConstant;
ySpeed *= frictionConstant;
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
sky.x = scrollX * 0.2;
sky.y = scrollY * 0.2;
if(carrotCollected == false){
if(player.hitTestObject(back.carrot)){
back.carrot.visible = false;
carrotGathered.visible = true; // HER
carrotCollected = true;
}
}
if(snowmanHappy == false){
if(carrotCollected == true){
if(player.hitTestObject(back.snowMan)){
carrotGathered.visible = false; // HER
back.snowMan.gotoAndPlay(2);
snowmanHappy = true;
}
}
}
if(openDoor == false){
if(snowmanHappy == true){
if(player.hitTestObject(back.snowMan)){
back.lockedDoor.gotoAndStop(2);
openDoor = true;
}
}
}
}
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
/*if(openDoor && player.hitTestObject(back.other.lockedDoor)){
gotoAndStop(2);
}*/
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
}
Your keyuphandler is engulfed in the KeyDownHandler. Check your { and } since you commented it out.

addEventListener() isn't detecting KEY_UP nor KEY_DOWN

My full code is
import flash.events.KeyboardEvent;
import flash.events.Event;
//init some variables
var speedX = 0;
var speedY = 0;
msg.visible = false;
var curLevel = 2;
var level = new Array();
var flagVar;
var won = false;
//Adding level platforms
for(var i = 0; i < numChildren; i++) {
if(getChildAt(i) is platform) {
level.push(getChildAt(i).getRect(this));
}
if(getChildAt(i) is flag) { flagVar = getChildAt(i).getRect(this); }
}
//Checking key presses
var kUp = false;
var kDown = false;
var kLeft = false;
var kRight = false;
var kSpace = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);
function kD(k:KeyboardEvent) {
trace("Key down - " + k.keyCode);
if(k.keyCode == 32) { kSpace = true; }
if(k.keyCode == 37 ) { kLeft = true; }
if(k.keyCode == 38) { kUp = true; }
if(k.keyCode == 39) { kRight = true; }
}
function kU(k:KeyboardEvent) {
trace("Key up - " + k.keyCode);
if(k.keyCode == 32) { kSpace = false; }
if(k.keyCode == 37) { kLeft = false; }
if(k.keyCode == 38) { kUp = false; }
if(k.keyCode == 39) { kRight = false; }
}
addEventListener(Event.ENTER_FRAME, loopAround);
function loopAround(e:Event) {
//horizontal movement
if(kLeft) {
speedX = -10;
} else if(kRight) {
speedX = 10;
} else {
speedX *= 0.5;
}
player.x += speedX;
//horizontal collision checks
for(var i = 0; i < level.length; i++) {
if(player.getRect(this).intersects(level[i])) {
if(speedX > 0) {
player.x = level[i].left - player.width;
}
if(speedX < 0) {
player.x = level[i].right;
}
speedX = 0;
}
}
//vertical movement
speedY += 1;
player.y += speedY;
var jumpable = false;
//Vertical collision
for(i = 0; i < level.length; i++) {
if(player.getRect(this).intersects(level[i])) {
if(speedY > 0) {
player.y = level[i].top - player.height;
speedY = 0;
jumpable = true;
}
if(speedY < 0) {
player.y = level[i].bottom;
speedY *= -0.5;
}
}
}
//JUMP!
if((kUp || kSpace) && jumpable) {
speedY=-20;
}
//Moving camera and other
this.x = -player.x + (stage.stageWidth/2);
this.y = -player.y + (stage.stageHeight/2);
msg.x = player.x - (msg.width/2);
msg.y = player.y - (msg.height/2);
//Checking win
if(player.getRect(this).intersects(flagVar)) {
msg.visible = true;
won = true;
}
//Check for next level request
if(kSpace && won) {
curLevel++;
gotoAndStop(curLevel);
won = false;
}
}
The section in question is
//Checking key presses
var kUp = false;
var kDown = false;
var kLeft = false;
var kRight = false;
var kSpace = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);
function kD(k:KeyboardEvent) {
trace("Key down - " + k.keyCode);
if(k.keyCode == 32) { kSpace = true; }
if(k.keyCode == 37 ) { kLeft = true; }
if(k.keyCode == 38) { kUp = true; }
if(k.keyCode == 39) { kRight = true; }
}
function kU(k:KeyboardEvent) {
trace("Key up - " + k.keyCode);
if(k.keyCode == 32) { kSpace = false; }
if(k.keyCode == 37) { kLeft = false; }
if(k.keyCode == 38) { kUp = false; }
if(k.keyCode == 39) { kRight = false; }
}
This was working fine last night, but today I moved it to a new keyframe and now it's not working. I'm not getting any errors (even if I debug). It just won't move the character or even show up in output.
I'm still quite new to as3, so I don't really know what to do.
Thanks in advance.
Edit: After playing with it a bit, I've found out that the reason it's not working is due to the menu. The menu has a single button and two text elements, which are fine. The code that I'm using on the menu is this:
import flash.events.MouseEvent;
stop();
var format:TextFormat = new TextFormat();
format.size = 26;
format.bold = true;
playGameButton.setStyle("textFormat", format);
stage.addEventListener(MouseEvent.CLICK, playGame);
function playGame(e:MouseEvent) {
if(e.target.name == "playGameButton") {
gotoAndStop(2);
}
}
If I use just gotoAndStop(2); it works fine, but with everything else it just goes to the second frame, and nothing else works after that.
Edit #2: I've narrowed it down even farther to the if statement itself.
if(e.target == playGameButton)
if(e.target.name == "playGameButton")
Both of those don't work. If I just remove the if statement all together it works perfectly fine.
there seems to be aproblem with this lines
if(getChildAt(i) is platform)
leads to error 1067: Implicit coercion of a value of type flash.display:MovieClip to an unrelated type Class
the rest of the code seems to be just fine
Try disabling your buttons mouseChildren.
playGameButton.mouseChildren = false;
Try e.currentTarget instead of e.target. From the documentation:
currentTarget : Object
[read-only] The object that is actively processing the Event object with an event listener.
target : Object
[read-only] The event target.
I'm not quite sure that this is your problem but the target vs currentTarget confusion has gotten me before.