简体   繁体   English

HaxeFlixel动画无法播放

[英]HaxeFlixel animations not playing

I'm trying to make a simple little game in HaxeFlixel, where you play as a ghost and you go around an apartment complex knocking on doors. 我正在尝试在HaxeFlixel中制作一个简单的小游戏,在那里您扮演鬼魂,然后到处敲门的公寓大楼四处逛逛。 There's a bit more to it than that, but that's the fundamental idea. 除此之外,还有很多,但这是基本思想。 Anyway, I've currently got the ghost moving from door to door, and knocking on them, but for some reason the animation where the tenant opens the door isn't triggering. 无论如何,我目前让鬼魂从一扇门移到另一扇门,然后敲它们,但是由于某些原因,租户打开门的动画没有触发。

Here is the state: 状态为:

package;

import flixel.addons.display.FlxBackdrop;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.FlxG;
import flixel.group.FlxTypedGroup;
import flixel.group.FlxTypedGroupIterator;
import flixel.text.FlxText;
import flixel.util.FlxPoint;
/**
 * ...
 * @author ...
 */

class GhostState extends FlxState
{
    var ghost:FlxSprite;
    var hall:FlxBackdrop;
    var wall:FlxSprite;
    var knock :Array<FlxText>;
    public var doors:FlxTypedGroup<Door>;
    public var speed = 0;
    public var inTransit:Bool;
    public var knockCount = 0;
    public var doneWithThisDoor = false;
    public var doorIndex = 0;

public function justPressed():Bool
{
    #if mobile
        var returnVal = false;
        for (touch in FlxG.touches.list)
        {
            returnVal = touch.justPressed;
        }
        return returnVal;
    #else
        return FlxG.mouse.justPressed;
    #end
}

public function pressed():Bool
{
    #if mobile
        var returnVal = false;
        for (touch in FlxG.touches.list)
        {
            returnVal = touch.pressed;
        }
        return returnVal;
    #else
        return FlxG.mouse.pressed;
    #end
}

public function justReleased():Bool
{
    #if mobile
        var returnVal = false;
        for (touch in FlxG.touches.list)
        {
            returnVal = touch.justReleased;
        }
        return returnVal;
    #else
        return FlxG.mouse.justReleased;
    #end
}

public function clickCoords():FlxPoint
{
    #if mobile
        var returnVal = new FlxPoint();
        var i=0;
        for (touch in FlxG.touches.list)
        {
            i++;
            returnVal.x += touch.screenX;
            returnVal.y += touch.screenY;
        }
        returnVal.x /= i;
        returnVal.y /= i;
        return returnVal;
    #else
        return new FlxPoint(FlxG.mouse.screenX, FlxG.mouse.screenY);
    #end
}

override public function create() 
{
    hall = new FlxBackdrop("assets/images/Stage3/hall wall.png", 0, 0, true, false);
    add(hall);

    doors = new FlxTypedGroup<Door> ();
    add(doors);

    #if web
        doors.add(new Door(((FlxG.width/2) - 259) + 465 + 175, (FlxG.height - 280) / 2, this, "assets/images/Stage3/door.png"));
    #else
        doors.add(new Door(((FlxG.width - 250-(175/2)-10) / 2) + 465 + 175, (FlxG.height - 280) / 2, this, "assets/images/Stage3/door.png"));
    #end

    ghost = new FlxSprite(FlxG.width / 2, FlxG.height / 2, "assets/images/Stage3/chicken ghost.png");
    ghost.loadGraphic("assets/images/Stage3/chicken ghost.png", true, 75, 100);
    ghost.animation.add("right", [0], 30, true);
    ghost.animation.add("forward", [1], 30, true);
    ghost.animation.add("back", [2], 30, true);
    add(ghost);

    speed = 0;
    super.create();

    knock = new Array();
    knock.push(new FlxText(ghost.x+25, ghost.y-35, -1, "*knock*", 20));
    knock.push(new FlxText(ghost.x+25, ghost.y - 85, -1, "*knock*", 20));
    for (member in knock)
    {
        member.color = 0x000000;
        add(member);
        member.kill();
    }

    nextDoor();
}

public function nextDoor()
{
    inTransit = true;
    if (ghost.x <= doors.members[doorIndex].x)
    {
        speed = 10;
        ghost.animation.play("right");
    }
    else
    {
        speed = 0;
        inTransit = false;
        doorIndex++;
        ghost.animation.play("forward");
    }
}

override public function update()
{
    hall.x -= speed;

    /*  var i = 0;
    while (i < doors.members.length)
    {
        var basic = doors.members[i++];

        if (basic != null && basic.exists && basic.active)
        {
            basic.update();
        }
    }*/

    if (inTransit)
    {
        nextDoor();
    }

    super.update();

    if (justPressed()&&!inTransit)
    {

        if (knockCount == 2)
        {
            knockCount = 0;
            for (member in knock)
            {
                member.kill();
            }
            doors.members[doorIndex-1].open();
            FlxG.watch.add(this,"doorIndex");
            nextDoor();
        }
        else 
        {
            knock[knockCount].revive();
            knockCount++;
        }
    }
}

}

And here is the Door class: 这里是Door类:

package;

import flixel.FlxSprite;
import flixel.FlxState;
import flixel.FlxG;

/**
 * ...
 * @author ...
 */
class Door extends FlxSprite
{
    var state:GhostState;
    var firstPass = true;

    public function new(X:Float=0, Y:Float=0, level:GhostState, ?SimpleGraphic:Dynamic) 
    {
        super(X, Y, SimpleGraphic);
        loadGraphic("assets/images/Stage3/door.png", true, 175, 250, false);
        animation.add("open", [0, 1, 2, 3, 4, 5], 30, false);
        animation.add("close", [5,4,3,2,1,0], 30, false);
        state = level;
        //this.animation.play("open");
        //state.doors.add(this);
    }

    public override function update():Void
    {
        if (firstPass)
        {
            if (isOnScreen())
            {
                state.doors.add(new Door(x + 465 + 175, (FlxG.height - 280) / 2, state));
                //state.add(new Door(x+465+175, (FlxG.height - 280) / 2, state));
                firstPass = false;
            }
        }

        this.x -= state.speed;
        if (this.x <= 0-this.width)
        {
            this.destroy();
        }
    }   

    public function open()
    {
        trace("open");
        animation.play("open", true, 0);
    }

    public function close()
    {
        animation.play("close", true, 0);
    }
} 

Some information abut the code, as I'm bad at remembering to comment: 代码旁边有一些信息,因为我不记得评论:

  1. doors is a group that contains all doors in the state doors是一个包含该状态下所有门的组
  2. doorIndex is the next door the ghost is supposed to move to next (so doorIndex - 1 is the door it's at right now) doorIndex是幽灵应该移动到的下一doorIndex - 1 (所以doorIndex - 1是它现在所在的门)

Why isn't it triggering? 为什么不触发? And how should I go about fixing this? 我应该如何解决呢?

update(elapsed:Float)函数具有一个elapsed参数,并且在要覆盖的函数内部,必须调用super.update(elapsed)父函数, super.update(elapsed)在此父函数中计算动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM