Hello! Noob Here trying to make a game...

214 views
Skip to first unread message

OzyManSpar

unread,
Jun 13, 2014, 3:56:20 AM6/13/14
to haxef...@googlegroups.com
Hello, Call Me Spar!
I am Fairly new to HaxeFlixel, and of course, trying to learn how to make a game. I've Been at it for a couple of days now, and not to toot my own horn, but so far i think i'm doing fairly well.
But attempts in trying to put  several mechanics/ commands/andotherstuff into my game, haven't gone well. so, i come here for assistance.
so....let me state what im trying to achieve.
First of all, I already have  A Player Sprite, I've Done the Basics and got him to run, and jump and show it's animations.
But what im now trying to do, is make the Sprite "Punch" and "Crouch"
For the punch, i can get the animation to play. but i have to hold down the Key for punching. and if i let go,it doesn't play the full animation. What i wanted to do, is make it where
The player presses the key once. and the full punching animation plays, and then after said sprite punches, you have to press the Key again, to Make the animation play again (i.e attacks). i've tried anyJustPressed and it makes the first frame play, but doesn't allow the full animation to play.
Now for the Crouch, is Similar to my problem with the punch. What im attempting to create here is:
Have the Player click S or Down key to make the player crouch. though what im trying here is more complicated, since im trying to make the animation play the Sprite playing a "getting into crouch" animation, and then have him kneeling/crouching, But again i don't want to have it where you hold down the key to make him stay crouching. Example: In Super Metroid, You press down and Samus' Sprite kneels, and stays that way until the player moves or presses up again.
So Player presses key down, Sprite goes into crouch mode, and eventually, i was thinking of trying to make a prone mode. maybe. where you press down again and the sprite goes prone, and thus press up makes you crouch again and after pressing up again the sprite is standing. *again, kinda like SuperMetroid, Replacing MorphBall With crawling...lol*
so, several things i've tried to do, is make the isCrouching bool and also a Crouch int. and one line of the many lines of code ive tried is :

if (Flx.keys.anyPressed(["S"]) ){
isCrouching = true;
}
if (isCrouching == true) {
animation.play("crouch") 
}
else (!Flx.leys.anyPressed(["S"])) {
isCrouching = false;
}

^this is an example of one of the lines of code i've tried putting in, it didn't work....lol, and i tried the same with crouch int.and i've also tried using anyJustPressed and then all of these other like, setups. but ya know....not exactly working.
So yeah, My question is....how do i do these things? 
or what code do i use?
and whatever else do i need.?
what am i doing wrong?
:D 

Gama11

unread,
Jun 13, 2014, 8:13:30 AM6/13/14
to haxef...@googlegroups.com
Hey,

your logic doesn't make too much sense as is. Since the second if-statement will always be executed when the first one is, it's exactly the same as writing:

if (FlxG.keys.anyPressed(["S"])) {
animation.play("crouch");
}

(assuming you don't need the isCrouching flag for anything else)

Can you show some more code? Animation setup, the rest of update(), maybe the spritehsheet of your player. As you describe it, the punching should work fine, as play() plays the full animation once called and does not force it to restart by default. I suspect another piece of code in your update() is preventing this from working.

Btw, if you're only checking for a single key, it's generally more convenient to use the key lists like so:

if (FlxG.keys.pressed.S)
if (FlxG.keys.justPressed.S)
if (FlxG.keys.justReleased.S)

OzyManSpar

unread,
Jun 13, 2014, 1:14:30 PM6/13/14
to haxef...@googlegroups.com
okay, i get what you mean
Here is my code. and i've included my Sprite in this reply too. 
package ;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxObject;
import flixel.input.keyboard.FlxKey;
import flixel.input.keyboard.FlxKeyboard;
import flixel.util.FlxPoint;
import flixel.tile.FlxTilemap;


class Player extends FlxSprite
{
  private var jumpPower:Int = 900;
  public var player:FlxSprite;
  private var jumplimit:Int = 0;
  private var punchlimit:Int = 0;
  private var crouching:Bool;
  private var crouch:Int = 0;
  private var S:Bool;
public function new(inX:Int, InY:Int) 
{
super(inX, InY);
loadGraphic("assets/images/Spar1.png", true, 20, 35);
width = 8;
height = 20;
offset = new FlxPoint(5, 1);
acceleration.y = 1000;
maxVelocity.set(200, 200);
drag.set(800, 800);
animation.add("Running", [0, 1, 2, 3, 4, 5], 7, true);
animation.add("Idle", [0]);
animation.add("Jump", [8]);
animation.add("Fall", [9]);
animation.add("punch", [10, 11, 12, ], 7);
animation.add("crouch", [13, 14, 15], 10,false);
setFacingFlip(FlxObject.RIGHT, false, false);
setFacingFlip(FlxObject.LEFT, true, false);
}
override public function update():Void 
{
acceleration.x = 0;
if (velocity.x == 0) {
animation.play("Idle");
}
if (FlxG.keys.anyPressed(["LEFT", "A"])) {
acceleration.x = -800;
facing = FlxObject.LEFT;
animation.play("Running");
} else if (FlxG.keys.anyPressed(["RIGHT", "D"])) {
acceleration.x = 800;
facing = FlxObject.RIGHT;
animation.play("Running");
}
if (!FlxG.keys.anyJustPressed(["W", "UP"]) && velocity.y == 0 ){
jumplimit = 0;
}
if (FlxG.keys.anyJustPressed(["W", "UP"]) && jumplimit < 2 ) { 
/*trace("jump");*/
jumplimit++;
velocity.y = -jumpPower;
}
if (velocity.y < 0) {
animation.play("Jump");
}else if (velocity.y > 0) {
animation.play("Fall");
}
if (FlxG.keys.anyPressed(["E"]) ){
animation.play("punch");
  
super.update();
}
}

i'm going to try and fix what you pointed out. 


Spar1.png

OzyManSpar

unread,
Jun 14, 2014, 10:53:18 PM6/14/14
to haxef...@googlegroups.com
bah, nothing's working.... : /

Gama11

unread,
Jun 15, 2014, 5:39:33 AM6/15/14
to haxef...@googlegroups.com
The problem is that when you're standing still, the "Idle" animation will always start playing as soon as you let go the E key. You need some sort of flag to prevent that from happening, like "punching". Then you also need to reset that flag when the animation has finished

Have a look at this pastebin, it comes pretty close to the behaviour you described I think.

OzyManSpar

unread,
Jun 16, 2014, 6:10:43 PM6/16/14
to haxef...@googlegroups.com
AH it worked! now that i see what the problem was, it seems so simple. thank you Gama!
Do you have any idea what i should do with Crouch? 
last night while trying to code the crouching, i got it to stay "crouched" but when i move or get up again. you can't crouch again.

Gama11

unread,
Jun 17, 2014, 4:31:02 AM6/17/14
to haxef...@googlegroups.com
Sounds like some of the flags you're using for crouching are not being reset properly? Could you show the code again?

OzyManSpar

unread,
Jun 19, 2014, 10:43:54 PM6/19/14
to haxef...@googlegroups.com
Well, before i saw your message, i changed my code where you have to hold down the "S" to hold it. 
but here is my code. anyway. just to see it, But if i still wanted to do what i originally intended, how would i go that way...
: / lol
Enter code here...package ;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxObject;
import flixel.input.keyboard.FlxKey;
import flixel.input.keyboard.FlxKeyboard;
import flixel.util.FlxPoint;
import flixel.tile.FlxTilemap;


class Player extends FlxSprite
{
  private var jumpPower:Int = 900;
  public var player:FlxSprite;
  private var jumplimit:Int = 0;
  private var punchlimit:Int = 0;
  private var punching:Bool = false;
  private var jumping:Bool = false;
  private var crouching:Bool;
  private var crouch:Int = 0;
  private var S:Bool;
  private var F:Bool;
  private var crouchi:Bool = false;
  private var sprinting:Bool;
  private var crouchmoving:Bool = false;
public function new(inX:Int, InY:Int) 
{
super(inX, InY);
loadGraphic("assets/images/Spar1.png", true, 20, 35);
width = 8;
height = 20;
offset = new FlxPoint(5, 1);
acceleration.y = 1000;
maxVelocity.set(200, 200);
drag.set(800, 800);
animation.add("Running", [0, 1, 2, 3, 4, 5], 7, true);
animation.add("Idle", [0]);
animation.add("Jump", [8]);
animation.add("Fall", [9]);
animation.add("punch", [10, 11, 12,0 ], 7, false);
animation.add("crouch1", [13, 14], 1, false);
animation.add("crouch", [13, 14, 15], 10,true);
animation.add("movingwCrouch",[24, 25, 26, 27], 6, true);
animation.add("sprint", [20, 21, 22, 23], 9, true);
animation.add("crouch3", [15]);
setFacingFlip(FlxObject.RIGHT, false, false);
setFacingFlip(FlxObject.LEFT, true, false);
animation.callback = animationCallback;
        
FlxG.watch.add(this, "punching");
}
override public function update():Void 
{
acceleration.x = 0;
if (!FlxG.keys.pressed.SHIFT) {
sprinting = false; 
}
if (FlxG.keys.pressed.SHIFT) {
sprinting = true;
}
if (velocity.x == 0 && !punching) {
animation.play("Idle");
}
if (FlxG.keys.anyPressed(["LEFT", "A"])) {
acceleration.x = -800;
facing = FlxObject.LEFT;
animation.play("Running");
crouchmoving = true;
} else if (FlxG.keys.anyPressed(["RIGHT", "D"])) {
acceleration.x = 800;
facing = FlxObject.RIGHT;
animation.play("Running");
crouchmoving = true; 
}
if (FlxG.keys.pressed.A && sprinting == true) {
velocity.x = -450;
animation.play("sprint");
} else if (FlxG.keys.pressed.D && sprinting == true) {
velocity.x = 450;
animation.play("sprint");
}
if (!FlxG.keys.anyJustPressed(["SPACE"]) && velocity.y == 0 ){
jumplimit = 0;
jumping = false; 
}
if (FlxG.keys.anyJustPressed(["SPACE"]) && jumplimit < 2 ) { 
/*trace("jump");*/
jumping = true;
jumplimit++;
velocity.y = -jumpPower;
}
if (velocity.y < 0) {
animation.play("Jump");
}
else if (velocity.y > 0) {
animation.play("Fall");
}
if (FlxG.keys.justPressed.E) {
animation.play("punch");
punching = true;
}
if (FlxG.keys.pressed.S && velocity.x == 0 ) {
crouchi = true; 
animation.play("crouch3");
}
if (FlxG.keys.pressed.S && crouchmoving == true && velocity.x != 0) { 
animation.play("movingwCrouch");
}
else if (FlxG.keys.pressed.S && crouchmoving == true && velocity.x != 0) {
animation.play("movingwCrouch");
}
super.update();
}

function animationCallback(name:String, frame:Int, frameIndex:Int):Void
        {
                FlxG.watch.addQuick("current anim", name);
                FlxG.watch.addQuick("current frame", frame);
               
                punching = (name == "punch" && !animation.curAnim.finished);
        }



Reply all
Reply to author
Forward
0 new messages