as3 if i press opposite movement keys, player stops - actionscript-3

If I press W and S aka up and down, at the same time. The player stops moving. Can't I make it so which key was pressed last can go towards that way, instead of stopping it all together. So regardless of which keys you press at the same time, the player just moves the way on the key you pressed last. Or if the player lets go of one of the buttons, the button that's still being held functions towards the way. That's how I would want it, but the player would stop all together. Like most games, there is fliud movement where the player wont stop moving unless you let go of all keys.
My code for keyboard movement on keydown:
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
}
In my enterframe:
if (isRight == true)
{
x += 5;
}
if (isLeft == true )
{
x -= 5;
}
if (isUp == true)
{
y -= 5;
}
if (isDown == true)
{
y += 5;
}

You should set isUp=false when setting isDown=true. And vice versa. Right now all you are doing is setting things as true, but you should set others as false when one becomes true.
Also, checking if(isDown) is a good enough condition for Boolean properties
Hope this helps.

Related

AS3 - Skip not Move

I'm making a frogger game in AS3 and when you press up, down, left and right, you "skip" a certain amount of space instead of gradually moving there like in most platformers now. How would I be able to do this?
Currently he moves up left, down and right on their respective keys. But he gradually moves there, not skipping the amount of space like in the real frogger.
Any ideas on how I could do this?
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.UP){
trace("frogger up");
player.y -= 5;
}
if(event.keyCode == Keyboard.DOWN){
trace("frogger down");
player.y += 5;
}
if(event.keyCode == Keyboard.LEFT){
trace("frogger left");
player.x -= 5;
}
if(event.keyCode == Keyboard.RIGHT){
trace("frogger right");
player.x += 5;
}
}
One way to do is to increase the X and Y with a certain amount like as Marty said in the comment, with 50.. or you could create two variables, let's name them xPosition & yPosition.
When holding down the keys, you would just increase/decrease these variables by 1, and then call a positioning function which would calculate the new position of the player depending on the xPosition and yPosition 's values and of course the grid sizes.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
var xPosition:int = 0;
var yPosition:int = 0;
var gridWidth:int = 32;
var gridHeight:int = 32;
function keyDownHandler(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.UP){
yPosition--;
repositionPlayer();
}
if(event.keyCode == Keyboard.DOWN){
yPosition++;
repositionPlayer();
}
if(event.keyCode == Keyboard.LEFT){
xPosition--;
repositionPlayer();
}
if(event.keyCode == Keyboard.RIGHT){
xPosition++;
repositionPlayer();
}
}
function repositionPlayer():void{
player.x = gridWidth*xPosition;
player.y = gridHeight*yPosition;
}
This gives you a big advantage in case you want to change the grid size.

how do i change my main player in flash?

im new about scripting and i want to make a little free shop, my game is about a running car that have to avoid objects, so in the shop i want to that when the user press select http://prntscr.com/270ws5 the main car that is this one http://prntscr.com/270y45 , become the car that they selected, thanks
//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyPressed:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 20;
//the current speed of the jump;
var jumpSpeed:Number = 0;
//adding a listener to mcMain which will make it move
//based on the key strokes that are down
player.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
player.x -= mainSpeed;
}
if(rightKeyDown){
player.x += mainSpeed;
}
if(upPressed || mainJumping){
mainJump();
gotoAndStop(2)
gotoAndStop(3)
gotoAndStop(4)
gotoAndStop(5)
}
}
//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upPressed = true;
gotoAndStop(2)
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upPressed = false;
gotoAndStop(2)
gotoAndStop(3)
gotoAndStop(4)
gotoAndStop(5)
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}
//jumping function
function mainJump():void{
//if main isn't already jumping
if(!mainJumping){
//then start jumping
mainJumping = true;
jumpSpeed = jumpSpeedLimit*-1;
player.y += jumpSpeed;
} else {
//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
jumpSpeed *= 1 - jumpSpeedLimit/75;
if(jumpSpeed > -jumpSpeedLimit/5){
jumpSpeed *= -1;
}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
jumpSpeed *= 1 + jumpSpeedLimit/50;
}
player.y += jumpSpeed;
//if main hits the floor, then stop jumping
//of course, we'll change this once we create the level
if(player.y >= stage.stageHeight - player.height){
mainJumping = false;
player.y = stage.stageHeight - player.height;
}
}
}
that is my car script, thanks a lot if someone can help me, to improve my script knowledge
You give a property to player's class of type MovieClip as it's movie clips you seem to have in your game to represent cars, which is then assigned an instance of a selected car. Then, whenever you want your player to gotoAndStop(), you address that to attached instance instead. Thus the instance will represent changeable graphics and the wrapper object (player) takes care of handling, collisions and other non-graphical stuff.
class Player extends Sprite {
private var _car:MovieClip; // the instance
public function Player() {
_car=new MovieClip();
addChild(_car);
}
public function get car():MovieClip { return _car; } // getter function
public function set car(value:MovieClip):void {
// setter function. We need to clean up our player from old car
if (!value) return; // null car - no wai
if (_car) removeChild(_car);
_car=value;
_car.x=0;
_car.y=0; // place car at zero relative to player
// this way you move the player, not the car,
// but MC playing is the car, not the player
addChild(_car);
}
...
}
Note that you can also relocate jump processing into Player class, so that you can just call player.jump() and don't care about the actual values, the player will then have to store the jumping constants, speeds, state (in midair or touching ground), etc etc.

charging Timer doesn't quite work?

I'm writing a game where I want the character to jump after holding the spacebar to charge their jump up for a bit and then the charge will get added into the jump afterwards.
However, the code that I currently have going only works around 50% of the time, and will almost indefinitely not work if the player is holding a directional movement button down at the same time.
function greenTimerHandler(e:TimerEvent){
if(spacePressed&&downBumping){
charge += 3;
if(charge >= 50){
charge = 50;
}
} else if(!spacePressed&& downBumping) {
greenSlimeJump(charge);
greenSlimeTimer.stop();
}
}
//And the jump function
function greenSlimeJump(greenCharge){
ySpeed = greenCharge * -1.5
slimeAmt -= Math.floor(greenCharge * 0.25);
charge = 0;
}
the downBumping is just a boolean collision value based on a hitTestPoint for the bottom of the player...
Is there any way that I can make it so that it actually runs the jump function more than it currently does? It will even keep the charge value for the next time it tries to jump and the slimeAmt value is never subtracted because the code just seems to completely skip the green jump function altogether.
Heres the downBumping code:
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y +
downBumpPoint.y, true)){
downBumping = true;
} else {
downBumping = false;
}
and heres the timer/ space pressed:
if(e.keyCode == 32 && slimeAction){
spacePressed = true;
if(slimeType == 1 && downBumping){
greenSlimeTimer.start();
}
}

AS3.0 if/else if evaluation behaviour for velocity control

Sorry if this is obvious to others, but I can't get my head around something in ActionScript 3.0 (huge n00b btw)
I have this code for controlling velocity:
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
When run, if I hold both LEFT and UP the Sprite moves Diagonally, but the fact that the two last conditionals (Keyboard.UP & Keyboard.DOWN) are elseifs should prevent them from being evaluated at all shouldnt it?
Is anyone able to shed some light on the behaviour?
When you press both buttons flash fires two independent events for each button. If you want to skip this case, you can make state flags (leftPressed, rightPressed, etc) for each button, change state in key handler and call the check motion method according to current states of each button.
If you don't want diagonal movement, set the velocity components to zero first like this:
public function keyDownHandler(event:KeyboardEvent):void
{
vx = vy = 0; ////
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}

fluidly dealing with multiple keypresses in ActionScript3

I'm currently building an engine for a platformer game at the moment, but I've noticed that ActionScript3 is having difficulty in keeping fluid when multiple keypresses are in use. For example;
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
} else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
} else if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
} else if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
So, as listed above, using the UP (or W, or Space) key triggers the player to jump (seperate onframe event handler handles gravity etc). Using the RIGHT (or D..) key triggers increases the player acceleration, which is again applied to the player in a seperate onframe event handler.
Everything works fine by itself - but the problem arises when multiple keystrokes are used. If a player starts to move to the right, and hits jump, he will cease accelerating. At the same time, he will not decelerate, as instructed in the Keyboard.UP method. Instead, he will maintain constant at his current rate, until the RIGHT key is hit again.
In short, it is as though Actionscript begins ignoring both the keyboard.down and keyboard.up methods for the RIGHT or LEFT movement keys, until they are no longer being pressed. This obviously causes for some very rigid gameplay - is there any solution anyone would be willing to share with me on this?
Your problem lies in the fact that your if conditionals are followed by if else conditionals. Drop the else and just have the if conditionals. Basically if the user holds down space then none of the other if conditionals are going to be tested since space is being held down and it's the first if statement. So just drop the else off of the if's. Just remove the if else conditionals that are testing keystrokes, not the conditionals inside of the if statements that deal with keystrokes.
Here is what your code should look like:
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
}
if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
}
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
}
if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
Something else you may notice is that when the UP key and RIGHT key are both being held down, the computer seems to freeze keyboard input, however when the W key and D key are being held down you can still press other keys and the computer will register their input. The answer to that question is here.
Update:
For the fluid part, instead of triggering something when a keystroke takes place, it is better to have a boolean variable such as keyUP or UP that holds a true if the key is down or false when the key is up. Then have a function onEnterFrame(event:Event):void {} that performs an action when keyUP is true. Like so:
import flash.events.*;
public class keyEvents extends MovieClip {
private var keyRIGHT:Boolean = false;
public function keyEvents():void
{
this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = true;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = false;
}
}
function onEnterFrame(event:Event):void
{
if(this.keyRIGHT) {
// This code is executed while the RIGHT arrow key is down.
}
}
}
If the above code does not work I think that your problem lies with your keyboard, not that it's broken or anything but the way it was made might be messing things up.
Let me know if this didn't help and I'll continue trying.