ActionScript 3 use multiple onKeyDown/onKeyUP - actionscript-3

Im trying to get a two plyer version of Snake running but i am having trouble getting the second snake to work, player 1 plays with w,a,s and d while player 2 uses the arrow keys. Player 1 with w,a,s and d is working player 2 with the arrows does not.
The code looks like this:
function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.A){
SnakeDirection = "left";
}else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
}else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
}else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.A) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.D) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.W ) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.S){
SnakeDirection = "";
}
}
/* function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT){
Snake2Direction = "left";
}else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
}else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
}else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.LEFT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.UP ) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.DOWN){
Snake2Direction = "";
}
}*/
The comments are there because it breaks the game, from my understanding the errors are because i can only use one onKeyUp/Down. If that is the case, is there another way?
Thanks!

Why do you want to use 2 functions for each event? Wouldn't it be much simpler if you just did the logic for the second player in the same function that also handles the logic of the first player?
Like this:
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "left";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
Keep in mind, you want to keep the if-blocks for the players separated, so that you can get multiple inputs at the same time.
Also as a side note, the else-if blocks in your onKeyUp function are unnecessary. They all do the same thing anyway.

Related

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

as3 - how to stop animation when both keys pressed?

My player stops moving when two keys are pressed at the same time. But the animation still moves. For example, if I press up and down at the same time or right and left at the same time.
On key down event listener:
if (event.keyCode == Keyboard.D)
{
isRight = true
}
if (event.keyCode == Keyboard.A)
{
isLeft = true
}
if (event.keyCode == Keyboard.W)
{
isUp = true
}
if (event.keyCode == Keyboard.S)
{
isDown = true
}
On key up event listener:
if (event.keyCode == Keyboard.D)
{
isRight = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.A)
{
isLeft = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.W)
{
isUp = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.S)
{
isDown = false
gotoAndStop(1);
}
On enterframe:
if (isRight == true)
{
x += 5;
play();
}
if (isLeft == true )
{
x -= 5;
play();
}
if (isUp == true)
{
y -= 5;
play();
}
if (isDown == true)
{
y += 5;
play();
}
If players goes x -= 1 and x += 1 it basically moves x += 0 overall. We can easily check that and stop animation in needed:
var iP:Point = new Point(x,y);//try to avoid creating new objects on frame interval
if (isRight) x += 5;
if (isLeft) x -= 5;
if (isUp) y -= 5;
if (isDown) y += 5;
if(!Point.distance(iP,new Point(x,y)) goToAndStop(1);
else play();
I don't see any checks to see if more than 1 key is being pressed?
surely you should introduce something like a keycount into the enterframe:
var count:uint = 0;
if (isRight == true){
count++
x += 5;
}
if (isLeft == true ){
count++;
x -= 5;
}
if (isUp == true){
count++;
y -= 5;
}
if (isDown == true){
count++
y += 5;
}
if (count > 1) {
isRight = isLeft = isUp = isDown = false;
gotoAndStop(1);
} else {
play();
}

AS3 Disabling keyboard text input

I'm making a virtual keyboard, and I want to find a good way to disable the actual keyboard.
For whatever reason my overall code doesnt work unless my text fields are input based.
I tried something simple with this, but it only works when its out of scope..
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
function onKeyEvent(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
if (e.keyCode == 65)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
else if (e.keyCode == 67)
{
trace(character);
}
else if (e.keyCode == 68)
{
trace(character);
}
else if (e.keyCode == 69)
{
trace(character);
}
else if (e.keyCode == 70)
{
trace(character);
}
else if (e.keyCode == 71)
{
trace(character);
}
else if (e.keyCode == 72)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
}
Try
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}

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.

Sequentially-specific combination of keys held for function?

I'm trying to give my character in a platformer game a movement mechanic in which holding the left key then also the right will cause the character to still move left but at a slower pace (i.e. movementSpeed/2) as if moon-walking (and visa versa):
public var leftKey:Boolean = false;
public var rightKey:Boolean = false;
public var upKey:Boolean = false;
public var leftFlag:Boolean = false;
function ifKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT && rightKey == false)
{
leftKey = true;
if (event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT)
{
leftFlag = true;
trace("leftFlag true");
}
}
if (event.keyCode == Keyboard.RIGHT && leftKey == false)
{
rightKey = true;
}
}
function ifKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftKey = false;
leftFlag = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightKey = false;
}
}
public function ifEnterFrame(event:Event):void
{
if (leftKey == true && leftFlag == false)
{
player1_mc.x -= mainSpeed;
trace("L");
}
if (rightKey == true && leftFlag == false)
{
player1_mc.x += mainSpeed;
trace("R");
}
if (leftKey == true && rightKey == true)
{
if (leftFlag == true)
{
player1_mc.x -= mainSpeed/2;
trace("L + R");
}
else
{
player1_mc.x += mainSpeed/2;
trace("R + L");
}
}
My output would look like this:
I hold left key
L
L
L
L
I let go of left key. Then,
I hold right key
R
R
R
R
I let go of right key. Then,
I hold right then also hold left
L
R
R+L
L
R
R+L
I let go of both. Then,
I hold left then also right
L
R
R+L
L
R
R+L
Though I know by my traces that the leftFlag is not being run, I've spent hours trying to figure out why to no avail. :(
I think your problem is this expression:
event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT
Though I am not familiar with actionscript, if it is anything like Java, the keyboard events are called once for each key press. "event" corresponds to only one key, not two different keys, and thus your expression will always return false.
The solution to your problem will probably involve something like this in both the key pressed and released functions.
if (event.keyCode == Keyboard.LEFT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
if (event.keyCode == Keyboard.RIGHT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
Hope that helps!