【发布时间】:2021-06-19 13:21:40
【问题描述】:
我正在使用 Phaser 3 和 typescript 制作平台游戏。我正在使用状态机和播放器控制器来处理播放器移动的逻辑,但似乎无法找到在播放器下降时播放下降动画('pablo_fall')的方法。我想也许检查velocity.y,如果它小于零进入下降状态,但我不能在this.sprite.body.velocity.y上使用
import Phaser from 'phaser'
import StateMachine from '../statemachine/StateMachine'
export default class playercontroller {
private sprite: Phaser.Physics.Matter.Sprite
private StateMachine: StateMachine
private cursors: Phaser.Types.Input.Keyboard.CursorKeys
constructor(sprite: Phaser.Physics.Matter.Sprite, cursors: Phaser.Types.Input.Keyboard.CursorKeys) {
this.sprite = sprite
this.cursors = cursors
this.createAnimations()
this.StateMachine = new StateMachine(this, 'player')
this.StateMachine.addState('idle', {
onEnter: this.idleOnEnter,
onUpdate: this.idleOnUpdate
})
.addState('walk', {
onEnter: this.walkOnEnter,
onUpdate: this.walkOnUpdate
})
.addState('jump', {
onEnter: this.jumpOnEnter,
onUpdate: this.jumpOnUpdate
})
.addState('doubleJump',{
onEnter: this.doubleJumpOnEnter,
onUpdate: this.doubleJumpOnUpdate
})
.setState('idle')
this.sprite.setOnCollide((data: MatterJS.ICollisionPair) => {
if (this.StateMachine.isCurrentState('jump')||this.StateMachine.isCurrentState('doubleJump')) {
this.StateMachine.setState('idle')
}
})
}
update(dt: number) {
this.StateMachine.update(dt)
}
private idleOnEnter() {
this.sprite.play('pablo_idle')
}
private idleOnUpdate() {
if (this.cursors.left.isDown || this.cursors.right.isDown) {
this.StateMachine.setState('walk')
}
const spaceJustPressed = Phaser.Input.Keyboard.JustDown(this.cursors.space)
if (spaceJustPressed) {
this.StateMachine.setState('jump')
}
}
private walkOnEnter() {
this.sprite.play('pablo_run')
}
private walkOnUpdate() {
const speed = 5;
if (this.cursors.left.isDown) {
this.sprite.flipX = true
this.sprite.setVelocityX(-speed)
}
else if (this.cursors.right.isDown) {
this.sprite.flipX = false
this.sprite.setVelocityX(speed)
}
else {
this.sprite.setVelocityX(0)
this.StateMachine.setState('idle')
}
const spaceJustPressed = Phaser.Input.Keyboard.JustDown(this.cursors.space)
if (spaceJustPressed) {
this.StateMachine.setState('jump')
}
}
private jumpOnEnter() {
this.sprite.setVelocityY(-6)
this.sprite.play('pablo_jump')
}
private jumpOnUpdate() {
const speed = 5;
let canDoubleJump = true;
const spaceJustPressed = Phaser.Input.Keyboard.JustDown(this.cursors.space)
if (spaceJustPressed) {
this.StateMachine.setState('doubleJump')
}
if (this.cursors.left.isDown) {
this.sprite.flipX = true
this.sprite.setVelocityX(-speed)
}
else if (this.cursors.right.isDown) {
this.sprite.flipX = false
this.sprite.setVelocityX(speed)
}
}
private doubleJumpOnEnter(){
this.sprite.setVelocityY(-6)
this.sprite.play('pablo_double_jump')
}
private doubleJumpOnUpdate() {
const speed = 5;
if (this.cursors.left.isDown) {
this.sprite.flipX = true
this.sprite.setVelocityX(-speed)
}
else if (this.cursors.right.isDown) {
this.sprite.flipX = false
this.sprite.setVelocityX(speed)
}
}
private createAnimations() {
this.sprite.anims.create({
key: 'pablo_idle',
frameRate: 20,
frames: this.sprite.anims.generateFrameNames('pablo_idle', {
start: 0,
end: 10,
prefix: 'Idle (32x32)-',
suffix: '.png'
}),
repeat: -1
})
this.sprite.anims.create({
key: 'pablo_run',
frameRate: 20,
frames: this.sprite.anims.generateFrameNames('pablo_run', {
start: 0,
end: 10,
prefix: 'Run (32x32)-',
suffix: '.png'
}),
repeat: -1
})
this.sprite.anims.create({
key: 'pablo_jump',
frameRate: 20,
frames: 'pablo_jump',
repeat: -1
})
this.sprite.anims.create({
key: 'pablo_fall',
frameRate: 20,
frames: 'pablo_fall',
repeat: -1
})
this.sprite.anims.create({
key: 'pablo_double_jump',
frameRate: 20,
frames: this.sprite.anims.generateFrameNames('pablo_double_jump', {
start: 0,
end: 5,
prefix: 'Double Jump (32x32)-',
suffix: '.png',
})
})
}
}```
【问题讨论】:
-
可能有助于详细说明为什么不能将
标签: javascript html typescript phaser-framework