as3 multiple key_down moving object - actionscript-3

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.

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

why doesn't this code detect diagonal motion?

"player" is an object that moves up , down , left and right when I press on arrow keys.
"player" doesn't move diagonally when I press up and left keys together or any other couple of keys.
I'm using Adobe flash CS5 and action script 3 ( As3 ), Would you tell me what the solution is ?
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);
function detectText(myevent:KeyboardEvent):void {
var up:Boolean = false;
var down:Boolean = false;
var left:Boolean = false;
var right:Boolean = false;
var speedOfplayer:Number = 5 ;
if (myevent.keyCode==39){
right = true ;
}
if (myevent.keyCode==37){
left = true ;
}
if (myevent.keyCode==38){
up = true ;
}
if (myevent.keyCode==40){
down = true ;
}
// if(right is true and left is not true)
if( right && !left ) {
player.x = player.x + speedOfplayer;
}
// if(up is true and down is not true)
if( up && !down ) {
player.y = player.y - speedOfplayer;
}
// if(down is true and up is not true)
if( down && !up ) {
player.y = player.y + speedOfplayer;
}
// if(down is true and up is not true)
if( left && !right ) {
player.x = player.x - speedOfplayer;
}
// Move diagonally
if( left && up && !right && !down ) {
player.y = player.y - speedOfplayer;
player.x = player.x - speedOfplayer;
}
if( right && up && !left && !down ) {
player.x = player.x + speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( left && down && !right && !up ) {
player.x = player.x - speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( right && down && !left && !up ) {
player.x = player.x + speedOfplayer;
player.y = player.y + speedOfplayer;
}
You may be overcomplicating things. This can be done in the following way. What is happening to the KEY_DOWN listener is that it will report the last key pressed. So you need a ENTER_FRAME listener to move the object. And because ENTER_FRAME will keep calling the function provided you need to capture if a key is released to reset the speed of the player to 0.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, onTick);
var speedX:uint = 0;
var speedY:uint = 0;
function onKeyDown(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP) {
speedY = -5;
}
if (key == Keyboard.DOWN) {
speedY = 5;
}
if (key == Keyboard.LEFT) {
speedX = -5;
}
if (key == Keyboard.RIGHT) {
speedX = 5;
}
}
function onKeyUp(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP || key == Keyboard.DOWN) {
speedY = 0;
}
if (key == Keyboard.LEFT || key == Keyboard.RIGHT) {
speedX = 0;
}
}
function onTick(event:Event):void {
player.x += speedX;
player.y += speedY;
}

as3 Object araba is not moving

Here is my code at below and im keep getting this error i've became a freak for 3 days please help me guys.I will be appriciate your comments.
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>
From
var araba:MovieClip=new araba();
var masa:MovieClip =new masa();
To
var araba:MovieClip=new MovieClip();
var masa:MovieClip =new MovieClip();
If araba and masa are actually your custom classes, then you forgot to import those 2 classes.
Use Flash Builder or Flash Develop for your development for auto imports and smart error checking.

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.