How do I make my character move diagonally in Actionscript 3.0 - actionscript-3

I'm having trouble making my figure move diagonally with the arrowkeys. I can make it so that it moves up, down, left and right but I'm unsure how to make it read that two buttons are pressed down at once and therefore it should move in a certain way. This it the code I have for the keypresses so far. Any tips are appreciated!
function keyPressHandler(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
up = true;
beetle_mc.rotation = 0;
beetle_mc.y -=speed;
up_mc.gotoAndStop(2);
break;
case Keyboard.DOWN:
down = true;
beetle_mc.rotation = 180;
beetle_mc.y +=speed;
down_mc.gotoAndStop(2);
break;
case Keyboard.LEFT:
left = true;
beetle_mc.rotation = 270;
beetle_mc.x -=speed;
left_mc.gotoAndStop(2);
break;
case Keyboard.RIGHT:
right = true;
beetle_mc.rotation = 90;
beetle_mc.x +=speed;
right_mc.gotoAndStop(2);
break;

Related

AS3 game development: attack movements not following suit of movement direction

I'm making a top-down RPG style game, very much a throwback to zelda. This is my code for the movement controls based on keyboard controls. The movement itself is pretty solid, no stutter-key syndrome, all animations are triggered correctly.
However, when the attack button (SPACE in this case) is pressed it triggers the appropriate frame but when the key is released the corresponding frame is still visible, until another direction key is pressed. So, when you attack it looks like he keeps his sword extended until another movement is made, and so on. This should only happen when the key is held down not when simply pressed. This is not the expected result. The result I'm looking for is an attack that triggers every time the key is pressed, creating the normal buttondown=attacking, buttonup = not attacking.
The attacks and movement now are based on a variable moving:int = 4; That way I can switch the attack position to the corresponding movement direction.
Any ideas on how to correct this issue?
import flash.events.Event;
import flash.events.KeyboardEvent;
character.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);// when key is pressed
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);//when key is released
stage.addEventListener(Event.ENTER_FRAME, MainLoop);
var moving:int = 4;
var movingDown:Boolean = false;
var movingUp:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;
function onKeyPress(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.DOWN : movingDown = true; moving = 1; break;
case Keyboard.UP : movingUp = true; moving = 2; break;
case Keyboard.RIGHT : movingRight = true; moving = 3; break;
case Keyboard.LEFT : movingLeft = true; moving = 4; break;
case Keyboard.SPACE : handleAttack(); break;
}
}
function onKeyRelease(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.DOWN: character.gotoAndStop(1); movingDown=false; moving = 1; break;
case Keyboard.UP: character.gotoAndStop(3);movingUp=false; moving = 2; break;
case Keyboard.LEFT: character.gotoAndStop(5);movingLeft=false; moving = 3; break;
case Keyboard.RIGHT: character.gotoAndStop(7);movingRight=false; moving = 4; break;
case Keyboard.SPACE: handleAttack(); break;
}
}
function handleAttack():void
{
switch (moving)
{
case 1: character.gotoAndStop(9); movingDown = false; break; //down
case 2: character.gotoAndStop(10); movingUp = false; break; //up
case 3: character.gotoAndStop(11); movingLeft = false; break; //left attacks
case 4: character.gotoAndStop(12); movingRight = false; break; //right
}
}
function MainLoop(e:Event):void
{
switch (true)
{
case movingDown: character.gotoAndStop(2); character.y+=4.5; break;
case movingUp: character.gotoAndStop(4); character.y-=4.5; break;
case movingLeft: character.gotoAndStop(6); character.x-=4.5; break;
case movingRight: character.gotoAndStop(8); character.x+=4.5; break;
}
}
I am kinda new myself but i think it's because both onKeyPress() and onKeyRelease() point to the same function handleAttack() and as such point to the same frame even when you release. Try making two seperate attack functions for each one.
e.g
attackPressed() for onKeyPress()
function attackPressed():void
{
switch (moving)
{
case 1: character.gotoAndStop(9); movingDown = false; break;
case 2: character.gotoAndStop(10); movingUp = false; break;
case 3: character.gotoAndStop(11); movingLeft = false; break;
case 4: character.gotoAndStop(12); movingRight = false; break;
}
}
and attackRelease() for onKeyRelease()
function attackRelease():void
{
switch (moving)
{
case 1:character.gotoAndStop(1); movingDown = false; break;
case 2:character.gotoAndStop(3); movingUp = false; break;
case 3: character.gotoAndStop(5); movingLeft = false; break;
case 4: character.gotoAndStop(7); movingRight = false; break;
}
}
Hope this helps.

Actionscript 3-- Trying to fix a delay that is occuring upon key press.

I'm trying to setup keyboard control for my game and ran into an interesting obstacle: When the player presses a key to move in a specific direction a delay in the physical movement occurs similar to the delay that happens when editing text.
For instance, When you hold down the "a" key (just as an example, of course it could be any key) and there is a second delay before the cursor will then register "aaaaaaa". The same problem is happening here, so when a direction key is pressed the frame animations begin before the physical movement starts. Which results in an animation that looks like the character is running in place and then finally starts moving after about a 1 or 2 seconds.
Any thoughts, ideas, or advice on a fix would be much appreciated. Thanks in advance, all.
import flash.events.Event;
import flash.events.KeyboardEvent;
character.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
stage.addEventListener(Event.ENTER_FRAME, onEnterThisFrame);
var moving:int = 4;
var animate:Boolean = false;
function onKeyPress(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case 37: moving = 1; character.gotoAndStop(6); character.x-=5; break; //left
case 38: moving = 2; character.gotoAndStop(4); character.y-=5; break; //up
case 39: moving = 3; character.gotoAndStop(8); character.x+=5; break; //right
case 40: moving = 4; character.gotoAndStop(2); character.y+=5; break; //down
case 32: handleAttack();
}
animate = false;
}
function onKeyRelease(e:KeyboardEvent):void
{
switch(moving)
{
case 1: character.gotoAndStop(6); break; //left
case 2: character.gotoAndStop(4); break; //up
case 3: character.gotoAndStop(8); break; //right
case 4: character.gotoAndStop(2); break; //down
}
animate = true;
}
function handleAttack():void
{
switch (moving)
{
case 1: character.gotoAndStop(11); break; //left
case 2: character.gotoAndStop(10); break; //up
case 3: character.gotoAndStop(12); break; //right
case 4: character.gotoAndStop(9); break; //down
}
}
function onEnterThisFrame(e:Event):void
{
if (animate == true)
{
switch (moving)
{
case 1: if(character.currentFrame == 6) character.gotoAndStop(5); break;
case 2: if(character.currentFrame == 4) character.gotoAndStop(3); break;
case 3: if(character.currentFrame == 8) character.gotoAndStop(7); break;
case 4: if(character.currentFrame == 2) character.gotoAndStop(1); break;
}
}
}
Have bools for each direction and change your onKeyPress and onKeyRelease to set these, then check them and move your character in your onEnterThisFrame function.

How to call AS3 Switch

I am struggling a bit with my switch statement (I've never used switch before). I have a hit test for when my object reaches the top or bottom of the stage. When this happens, I want to switch states (the game in question is a simple platformer that allows the player to switch the gravity when they hit a new surface. Below is my current code:
{
...
if(player.hitTestObject(bottom)) {
//Switch state to normal
}
if(player.hitTestObject(top)) {
//Switch state to inverted
}
}
switch (myGrav){
case "NORMAL":
trace("Normal")
player.gotoAndPlay(1);
oktoJump = false;
player.y = 376.5;
case "INVERTED":
trace("Inverted")
player.gotoAndPlay(8);
oktoJump = false;
player.y = 12;
}
Thanks!
Cases within your switch statement are missing a break; therefore, code will continue to execute through the switch statement.
This should be:
var myGrav:String = "NORMAL";
if (player.hitTestObject(bottom))
myGrav = "NORMAL";
if (player.hitTestObject(top))
myGrav = "INVERTED";
switch (myGrav)
{
case "NORMAL":
trace("Normal")
player.gotoAndPlay(1);
oktoJump = false;
player.y = 376.5;
break;
case "INVERTED":
trace("Inverted")
player.gotoAndPlay(8);
oktoJump = false;
player.y = 12;
break;
}
I prefer less variables when I can get away with it.
switch (true){
case (player.hitTestObject(bottom)):
trace("Normal")
player.gotoAndPlay(1);
oktoJump = false;
player.y = 376.5;
break;
case (player.hitTestObject(top)):
trace("Inverted")
player.gotoAndPlay(8);
oktoJump = false;
player.y = 12;
break;
}

Camera following object

I am fairly new to coding. I am was wondering if there was a way so when I move my object with keys I can get the camera to keep on my object so I can move around a larger world than just my viewport.
I have tried to move the world around instead of my object but for what I am doing it makes my coding a lot more difficult
Here is the code I have.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
function keydown (event:KeyboardEvent):void{
switch (event.keyCode){
case Keyboard.LEFT :
ball.x -= 10;
break ;
case Keyboard.RIGHT :
ball.x += 10;
break;
case Keyboard.UP :
ball.y -= 10;
break;
case Keyboard.DOWN :
ball.y += 10;
break;
default :
break;
}
}
you could have the world and the character in a holding sprite/movieclip
then as you move the character, you can set the position of the holder in the opposite direction.
you can use global to local to get the position of the ball, and then can even apply some movement smoothing
here's some code that should work easy enough
you need a movieclip called holder inside that is ball , the holder would also have your world and world assets. And as long as there's no scaling you don't need to use globalToLocal
var middlePt:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);
var pt:Point = new Point(holder.ball.x, holder.ball.y);
var destPoint:Point = new Point(-pt.x + middlePt.x, -pt.y + middlePt.y);
addEventListener(Event.ENTER_FRAME,enterFrame);
function enterFrame(e:Event):void{
holder.x = holder.x*0.5 + destPoint.x*0.5;
holder.y = holder.y*0.5 + destPoint.y*0.5;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
function keydown (event:KeyboardEvent):void{
switch (event.keyCode){
case Keyboard.LEFT :
holder.ball.x -= 10;
break ;
case Keyboard.RIGHT :
holder.ball.x += 10;
break;
case Keyboard.UP :
holder.ball.y -= 10;
break;
case Keyboard.DOWN :
holder.ball.y += 10;
break;
default :
break;
}
middlePt = new Point(stage.stageWidth/2, stage.stageHeight/2);
pt = new Point(holder.ball.x, holder.ball.y);
destPoint = new Point(-pt.x + middlePt.x, -pt.y + middlePt.y);
}

ActionScript 3 - Walls don't collide properly

For several weeks I'm been trying to make my topdown game. It went well for some time, but then at some point I wanted to create a scrolling map with walls everywhere. Now, to make it easy to create the map (and add more later) I made a class called "Wall" which I will hit test. This works, when it hits, the map must stop scrolling. It does, so good so far.
Now, when the player moves away from the object, I want the map to be able to scroll again, this works too, but now the player can't move to the side the player came from. I know this is because I need to define the sides, where the player enters, in order tell the game which movement must be set to zero at that point.
You can see the code here:
public function AddWalls(player:MovieClip)
{
WallObjects = new Array();
for (var i:int = 0; i < this.numChildren; i++)
{
var mc = this.getChildAt(i);
if (mc is Wall)
{
var wallobj:Object = new Object();
wallobj.mc = mc;
wallobj.leftside = mc.x;
wallobj.rightside = mc.x + mc.width;
wallobj.topside = mc.y;
wallobj.bottomside = mc.y + mc.height;
wallobj.width = mc.width;
wallobj.height = mc.height;
WallObjects.push(wallobj);
}
}
}
public function EnableCollisionWithWalls():void
{
for (var k:int = 0; k < WallObjects.length; k++)
{
//if (player.y > WallObjects[k].topside && player.y < WallObjects[k].bottomside && player.x > WallObjects[k].leftside && player.x < WallObjects[k].rightside)
if (player.hitTestObject(WallObjects[k].mc))
{
if (player.x > WallObjects[k].leftside && player.x < WallObjects[k].leftside+15)
{
Lefthit = true;
trace(DebugVar);
DebugVar++;
player.x = WallObjects[k].leftside;
Scroll_x = 0;
}
else
if ( player.x < WallObjects[k].leftside -1 || (player.y > WallObjects[k].leftside ))
{
Lefthit = false;
}
if (player.hitTestObject(derp))
{
Lefthit = false;
}
}
}
}
public function EnableMovement():void
{
map.x += Scroll_x;
map.y += Scroll_y;
for (var i:int = 0; i < this.numChildren; i++)
{
var mc = this.getChildAt(i);
if (mc is Wall)
{
mc.x += Scroll_x;
mc.y += Scroll_y;
}
}
}
public function MovementKeysDown(move:KeyboardEvent):void
{
var Speed:int = -5;
switch (move.keyCode)
{
case 37: // venstre knap
Scroll_x = -Speed;
break;
case 38: // op
Scroll_y = -Speed;
break;
case 39: // højre knap
Scroll_x = Speed;
if (Lefthit)
{
Scroll_x = 0;
}
break;
case 40: // ned
Scroll_y = Speed;
break;
default:
}
}
public function MovementKeysUp(move:KeyboardEvent):void
{
switch (move.keyCode)
{
case 37:
Scroll_x = 0;
break;
case 38:
Scroll_y = 0;
break;
case 39:
Scroll_x = 0;
break;
case 40:
Scroll_y = 0;
break;
default:
}
}
Might be some syntax errors (since I removed some code in this editor).
You can see the current version here.
In this version the scroll keeps on going. I did come up with a "fix" for it, by check if the player was 1 pixel away from the movieclip, inside the hit test (which for some reason works, which I guess it shouldn't since it doesn't hit anymore) and then setting the Lefthit to false. However this is not a good solution and if you continue up or down away from the movieclip, you are still not able to go right anymore...
I've been baffled by this for a long time, so I thought it was about time I asked for help. I couldn't find anything on how to control movement in a top-down game, with a scrolling map + wall :/
The simplest (but not most resource friendly) solution (if you anyway have a single storage for walls) is iterating through the walls and instead of using the Flash default hitTest (I don't like the way it works since ActionScript 2) - just check the coordinates and if you see that there's going to be a collision on the next simulation step - handle it according to the game logic.
The most useful optimization for this algorithm is creating a filter/data structure for getting only walls that are near to the player and so can be affected to the test for collisions.