as3 Object araba is not moving - actionscript-3

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.

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

Can't locate a 1026 error requesting assistance with location

I can't seem to find the source of the 1026 error:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.*;
import flash.display.Sprite;
import fl.controls.ProgressBar;
stop();
var NueMCSpeed:Number = 10;
var _movingUp:Boolean = false;
var _movingDown:Boolean = false;
var _movingLeft:Boolean = false;
var _movingRight:Boolean = false;
var _focused:Boolean = false;
var _attacking:Boolean = false;
var ShootAllow:Boolean = true;
var timer:Number = 2;
var shotN:Number = 0;
var SHOTINDEX:Number = 0;
NueMC.gotoAndStop("NueG");
var _root:Object;
var speed:int = 10;
Mainp();
function Mainp()
{
//var NueMC:MovieClip;
// add listeners
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
}
function createPlayer():void
{
var NueMC:MovieClip;
NueMC.x = stage.stageWidth / 2;
NueMC.y = stage.stageHeight / 2;
stage.addChild(NueMC);
}
function myOnPress(event:KeyboardEvent):void
{
switch ( event.keyCode )
{
case Keyboard.UP :
_movingUp = true;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.DOWN :
_movingDown = true;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.LEFT :
_movingLeft = true;
NueMC.gotoAndStop("NueL");
break;
case Keyboard.RIGHT :
_movingRight = true;
NueMC.gotoAndStop("NueR");
break;
case Keyboard.SHIFT :
_focused = true;
break;
case Keyboard.Z :
_attacking = true;
//timer++
//if(timer == 3){
//shoot();
//timer = 0
//}
break;
}
}
function myOnRelease(event:KeyboardEvent):void
{
switch ( event.keyCode )
{
case Keyboard.UP :
_movingUp = false;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.DOWN :
_movingDown = false;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.LEFT :
_movingLeft = false;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.RIGHT :
_movingRight = false;
NueMC.gotoAndStop("NueG");
break;
case Keyboard.SHIFT :
_focused = false;
break;
case Keyboard.Z :
_attacking = false;
break;
}
}
function enterFrameHandler(event:Event):void
{
if (_focused == true)
{
NueMCSpeed = 5;
ShootAllow = true
}
if (_focused == false)
{
NueMCSpeed = 10;
}
if ( _movingLeft && !_movingRight )
{
NueMC.x -= NueMCSpeed;
}
if ( _movingRight && !_movingLeft )
{
NueMC.x += NueMCSpeed;
}
if ( _movingUp && !_movingDown )
{
NueMC.y -= NueMCSpeed;
}
if ( _movingDown && !_movingUp )
{
NueMC.y += NueMCSpeed;
}
if(NueMC.y < 165)
{
NueMC.y = 165
}
if(NueMC.y > 885)
{
NueMC.y = 885
}
if(NueMC.x > 520)
{
NueMC.x = 520
}
if(NueMC.x < 10)
{
NueMC.x = 10
}
// Move diagonally
if ( _movingLeft && _movingUp && !_movingRight && !_movingDown )
{
}
if ( _movingRight && _movingUp && !_movingLeft && !_movingDown )
{
}
if ( _movingLeft && _movingDown && !_movingRight && !_movingUp )
{
}
if ( _movingRight && _movingDown && !_movingLeft && !_movingUp )
{
}
}
var pIsDead:Boolean = false;
var LSP:Number = .1;
var NP:Number = .0001;
var vx:Number = NueMC.x;
var vy:Number = NueMC.y;
var BHPN:Number = BHP.scaleX
if(ShootAllow == true && _attacking == true)
{
stageRef.addChild(new Bullet(stageRef, x + vx, y + vy));
ShootAllow = false;
}
if (NueMC.NHB.hitTestObject(stage.DBS.DBHB) && NueLB.scaleX > .1)
{
NueLB.scaleX -= LSP;
Score -=10000;
NueMC.gotoAndStop("NueDie");
pIsDead = true;
}
And then in an .as file I have:
package
{
import flash.display.MovieClip;
import fl.controls.ProgressBar;
import flash.display.Stage;
import flash.events.Event;
public class Bullet2 extends flash.display.MovieClip
{
private var stageRef2:Stage;
private var bulletSpeed:Number = 20;
public function Bullet2(stageRef:Stage, xd:Number, yd:Number):void
{
this.stageRef = stageRef2;
this.x = xd;
this.y = yd;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event):void
{
y -= bulletSpeed;
if (y < 125)
{
removeSelf();
}
}
private function removeSelf():void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
{
stageRef.removeChild(this);
}
}
}
}
I need to finish this program soon so help is greatly appreciated. PS code is long as I have no idea where the error is since flash doesn't want to give me the line.
The error is in this line: function Mainp() The problem is that it is located on the timeline, you cannot place constructors on the timeline. You should find the main *.as file and place that Mainp() in there, inside the class Mainp definition.

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.

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.