Show Posts
|
|
Pages: [1]
|
|
2
|
Developer / Technical / Re: Requesting help with "Input" issues in AS3
|
on: November 20, 2012, 12:10:25 PM
|
|
Thanks to all of you for replying and helping me out!
I'm trying to pick up programming after a seven year hiatus, and even back then I didn't really 'get' it, so especially John's post really helps me a lot. I don't want to make mistakes that'll bite me later on, I much prefer doing things the hard way if it helps me in the long run.
I'm gonna run the code and see if it works.
Thanks a lot again!
|
|
|
|
|
3
|
Developer / Technical / Re: Requesting help with "Input" issues in AS3
|
on: November 20, 2012, 11:35:22 AM
|
Ah, thanks for the quick reply. It's actually declared false at another point in the code, here, let me paste the whole thing. package src.objects { import flash.geom.Point; import net.flashpunk.Entity; import net.flashpunk.FP; import net.flashpunk.graphics.Spritemap; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.Sfx; import src.Global; /** * ... * @author Noel Berry */ public class Player extends Entity { [Embed(source = '../../assets/sandra_Running.png')] private const IMAGE:Class; public var sprite:Spritemap = new Spritemap(IMAGE, 32, 32, null); [Embed(source="../../assets/viola01.mp3")] private const JUMP01:Class; public var jump01:Sfx = new Sfx(JUMP01); public var speed:Point = new Point(0, 0); public var gravity:Number = 0.2; public var floored:Boolean = false; public var runspeed:int = 2; public var pDir:int = 1; public var pRunning:Boolean = false; public function Player(x:int, y:int) { //set position and sprite super(x, y, sprite); //add the animations sprite.add("ground", [0], 0, true); sprite.add("pRunning", [1, 2, 3, 2], 0.06, true); sprite.add("pRunningfromjumping", [2, 3, 2, 1], 0.06, true); sprite.add("air", [1], 0, true);
//set the hitbox setHitbox(10, 31); Input.define("up", Key.W, Key.UP); Input.define("down", Key.S, Key.DOWN); Input.define("left", Key.A, Key.LEFT); Input.define("right", Key.D, Key.RIGHT); Input.define("jump", Key.SPACE); Input.define("attacka", Key.SHIFT); Input.define("attackb", Key.CONTROL); Input.define("attackc", Key.NUMPAD_0); } override public function update():void { super.update(); if (Input.pressed("left") && pRunning == false) { playerRun( -1, true); } if(Input.pressed("right") && pRunning == false) { playerRun(1, false); } //very basic friction if (!Input.check("right") && !Input.check("left")) { speed.x = 0; pRunning = false; if (floored == true) { sprite.play("ground"); } } //gravity if (!collide("Floor", x, y + 1)) { speed.y += gravity; sprite.play("air"); floored = false; } //update sprite, if we're on a floor or not if (collide("Floor", x, y + 1)) { if (floored == false) { //We just landed! if (pRunning == true) { if (Input.check("right") || Input.check("left")) { sprite.play("pRunningfromjumping"); } } } floored = true; //jump if (Input.check("jump")) {speed.y = - 4; jump01.play();} }
//move for (var i:int = 0; i < Math.abs(speed.x); i ++) { if (!collide("Floor", x + FP.sign(speed.x), y)) { x += FP.sign(speed.x);} else { speed.x = 0; } } for (i = 0; i < Math.abs(speed.y); i ++) { if (!collide("Floor", x, y + FP.sign(speed.y))) { y += FP.sign(speed.y); } else { speed.y = 0; } } //prevents falling offscreen if(x > FP.screen.width-width) { x = FP.screen.width-width; } else if(x < 0) { x = 0; } if(y > FP.screen.height-height) { y = FP.screen.height-height; } else if(y < 0) { y = 0; } } public function playerRun(dir:int, flip:Boolean) { pRunning = true; sprite.play("pRunning"); speed.x += runspeed * dir; sprite.flipped = flip; } }
}
if (!Input.check("right") && !Input.check("left")) { speed.x = 0; pRunning = false; if (floored == true) { sprite.play("ground"); } }
|
|
|
|
|
4
|
Developer / Technical / Requesting help with "Input" issues in AS3
|
on: November 20, 2012, 11:23:20 AM
|
Hey tigSource. I recently started working on a small project and I'm having some problems with the character movement. I've put in moving left and right, but when you're moving in one direction and then press the button for the opposite one (while still holding the original button down) and then release the original one (for example, LEFT -> RIGHT -> RELEASE LEFT), the character keeps moving in that direction (LEFT in the example). Here's the code: if (Input.pressed("left") && pRunning == false) { playerRun( -1, true); }
if(Input.pressed("right") && pRunning == false) { playerRun(1, false); }
........
public function playerRun(dir:int, flip:Boolean) { pRunning = true; sprite.play("pRunning"); speed.x += runspeed * dir; sprite.flipped = flip; }
This is basically scrub-level issues, but I'm new at programming and I would really appreciate the help! Thanks!
|
|
|
|
|