我已经在stackoverflow上找到了所有地方,但是找不到任何答案。
Uncaught TypeError:this.rsGame不是一个函数(关于this.addEnemy的相同)
let game = new Phaser.Game(600,600);
let speed = 500;
let scyla = {
preload: () => {
game.load.image('bg', 'assets/bg.png');
game.load.image('pl', 'assets/pl.png');
game.load.image('enemy', 'assets/enemy.png');
},
create: () => {
game.physics.startSystem(Phaser.Physics.ARCADE)
game.add.sprite(0,0, 'bg');
this.player = game.add.sprite(300, 500, 'pl');
this.player.anchor.set(0.5);
game.physics.arcade.enable(this.player);
this.cursors = game.input.keyboard.createCursorKeys();
this.enemies = game.add.group();
// this.timer = game.time.events.loop(200, this.addEnemy(), this);
},
update: () => {
this.player.body.velocity.x = 0;
this.player.body.velocity.y = 0;
if (this.cursors.left.isDown)
this.player.body.velocity.x = speed * -1;
if (this.cursors.right.isDown)
this.player.body.velocity.x = speed;
if (this.cursors.up.isDown)
this.player.body.velocity.y = speed * -1;
if (this.cursors.down.isDown)
this.player.body.velocity.y = speed;
if (this.player.inWorld === false)
this.rsGame();
},
rsGame: () => {
game.state.start('scyla');
},
addEnemy: () => {
let enemy = game.add.sprite(300, 100, 'enemy');
game.physics.arcade.enable(enemy);
enemy.body.gravity.y = 200;
this.enemies.add(enemy);
enemy.checkWorldBounds = true;
enemy.outOfBoundsKill = true;
}
}
game.state.add('scyla', scyla);
game.state.start('scyla');
我尝试过像
这样的事情let self = this
无论如何都会返回windows对象。这与封闭有关,但我并不完全理解
不知道如何解决这个问题:/
答案 0 :(得分:1)
箭头功能将this
设置为 lexical 范围。您尝试访问scyla
对象,但箭头功能正在将其设置为window
(或者在您声明this
时scyla
等于scyla
)。
直接引用scyla.rsGame();
:
update: function() {
...
if (this.player.inWorld === false)
this.rsGame();
}
或使用标准函数表达式编写方法:
update() {
...
if (this.player.inWorld === false)
this.rsGame();
}
或简写方法声明:
<td class="X">
<img src="/a/b/c/d.gif">
<td>
答案 1 :(得分:0)
箭头函数保留preload: function preload () {
// etc
}
的声明值。
使用常规函数表达式。不要使用箭头功能。
Tuc={"i":["o"],"love":["wau"],"you":["uo"],"me":["ye"],"my":["yem"],"mine":["yeme"],"are":["sia"]}
Eng = {t: e for t, e in Tuc.items()}
print "ENG"
print Eng
print "TUC"
print Tuc
phrase=True
reverseLookup = False
while phrase == True:
translation = str(raw_input("Enter content for translation.\n").lower())
input_list = translation.split()
for word in input_list:
#English to Tuccin
if word in Tuc:
print ("".join(Tuc[word]))+" *English>>Tuccin"
#Tuccin to English
elif word in Eng:
print ("".join(Eng[word]))+" *Tuccin>>English"
else:
print word+" *Word Not Stored"
答案 2 :(得分:0)
箭头函数具有词法范围this
。您需要在您的案例中使用常规函数,以便update: () => {
像往常一样绑定。变化:
update: function ( ) {
要:
scyla
,类似于{{1}}的其他属性。