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

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

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

as3 multiple key_down moving object

My object "araba" is not moving when KEY_DOWN happening waiting for your comments thx !
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.Bitmap;
stage.addEventListener(KeyboardEvent.KEY_DOWN, basili);
stage.addEventListener(KeyboardEvent.KEY_UP, degil);
stage.addEventListener(Event.ENTER_FRAME, giris);
var yukari:Boolean = false;
var asagi:Boolean = false;
var sola:Boolean = false;
var saga:Boolean = false;
var hiz:Number = 5;
var araba:MovieClip=new araba();
var masa:MovieClip =new masa();
function giris(event:Event):void
{
if ( sola && !saga ) {
araba.x -= hiz;
araba.rotation = 270;
}
if( saga && !sola ) {
araba.x +=hiz;
araba.rotation = 90;
}
if( yukari && !asagi ) {
araba.y -= hiz;
araba.rotation = 0;
}
if( asagi && !yukari ) {
araba.y += hiz;
araba.rotation = 180;
}
if( sola && yukari && !saga && !asagi ) {
araba.rotation = 315;
}
if( saga && yukari && !sola && !asagi ) {
araba.rotation = 45;
}
if( sola && asagi && !saga && !yukari ) {
araba.rotation = 225;
}
if( saga && asagi && !sola && !yukari) {
araba.rotation = 135;
}
if( araba.y < masa.y ){
araba.y = masa.height;
}
if( araba.y > masa.height ){
araba.y = masa.y;
}
if( araba.x < masa.x ){
araba.x = masa.width;
}
if( araba.x > masa.width ){
araba.x = masa.x;
}
}
function basili (event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = true;
break;
case Keyboard.DOWN:
asagi = true;
break;
case Keyboard.LEFT:
sola = true;
break;
case Keyboard.RIGHT:
saga = true;
break;
}
}
function degil(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = false;
break;
case Keyboard.DOWN:
asagi = false;
break;
case Keyboard.LEFT:
sola = false;
break;
case Keyboard.RIGHT:
saga = false;
break;
}
}
<code>
I've looked at your code and it looks fine to me, but why don't you try tracing and see what happens?
private var leftKeyIsDown:Boolean;
private var rightKeyIsdown:Boolean
private var downKeyIsDown:Boolean;
private var upKeyIsDown:Boolean;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); //when key is down, go to this function
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); //when key is up, go to this function
gameloop();
private function gameLoop(e:Event):void //this function runs 30 times every second, due to 30fps
{
playerControl();
}
private function keyDown(e:KeyboardEvent):void
{
//trace Event and KeyCode, when we trace this we get I.D number from key
//trace(e.keyCode);
if (e.keyCode == 37)
{
//left key is pressed
leftKeyIsDown = true;
}
if (e.keyCode == 38)
{
//up key is pressed
upKeyIsDown = true;
}
if (e.keyCode == 39)
{
//right key is pressed
rightKeyIsdown = true;
}
if (e.keyCode == 40)
{
//down key is pressed
downKeyIsDown = true;
}
}
private function keyUp(e:KeyboardEvent):void
{
//if our left key is released
if (e.keyCode == 37)
{
//left key is released
leftKeyIsDown = false;
}
if (e.keyCode == 38)
{
//up key is released
upKeyIsDown = false;
}
if (e.keyCode == 39)
{
//right key is released
rightKeyIsdown = false;
}
if (e.keyCode == 40)
{
//down key is released
downKeyIsDown = false;
}
}
private function playerControl():void
{
//if left is currently down
if (leftKeyIsDown)
{
//move ship to the left
ship.x -= 5; //subtract 5 from ships position evey loop,
}
//if right is currently down
if (rightKeyIsdown)
{
//move ship to the right
ship.x += 5; //subtract 5 from ships position evey loop,
}
//if up is currently down
if (upKeyIsDown)
{
//move ship up
ship.y -= 5; //subtract 5 from ships position evey loop,
}
//if left is currently down
if (downKeyIsDown)
{
//move ship down
ship.y += 5; //subtract 5 from ships position evey loop,
}
}
replace ship with your object.
This is a template, i've commented too.
Please tell me if this is useful, it doesn't fix your code, I know.
But hopefully you can adapt this to it.

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

bitmap hit testing error

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.

As3 How to flip a movieclip to face movement direction?

Working on a maze game. When the leftkey is pressed the movieclip (char) should turn 90 degrees to the left.
Correct me if i'm wrong but I thought I could use this code;
char.scaleX *= -1;
However, the most important thing is that the character doesnt go through the walls of the maze.
And I think thats my problem for implementing the code above.
Because it doesnt work properly when i put in here;
if(!mazehit) {
char.y += speed;
char.scaleX *= -1;
}
My question to you is, where do I have to put the code to flip the movieclip?
var leftArrow, rightArrow, upArrow, downArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow) {
for(var i:int = 0; i < speed; i++) {
if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x -= speed;
}
} else if (rightArrow) {
for(var j:int = 0; j < speed; j++) {
if(bounds.hitTestPoint(char.x + charRadius + j, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x += speed;
}
} else if (upArrow) {
for(var k:int = 0; k < speed; k++) {
if(bounds.hitTestPoint(char.x, char.y - charRadius - k, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y -= speed;
}
} else if (downArrow) {
for(var m:int = 0; m < speed; m++) {
if(bounds.hitTestPoint(char.x, char.y + charRadius + m, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y += speed;
}
}
}
thank you for your time
I would update the direction depending on the speed:
char.scaleX = (speed > 0) ? 1 : -1;
Or, by the keys that have been pressed:
if(keyLeft && !keyRight)
{
char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
char.scaleX = 1;
}
else
{
// keep current direction
}