TypeScript Errors #6308
-
Version
DescriptionWhen trying to recreate the game using the website's guide, I encountered a couple of TypeScript errors. In a section while iterating the stars it shows const stars = this.physics.add.group({
key: "star",
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 },
});
stars.children.iterate((child) => {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8)); // Here it says `setBounceY` doesn't exist.
return null;
}); And just after that, in the tutorial, where it shows how to detect overlap, the doc shows to use But it shows a type error when I pass Looking at the type it look like it expects something else: I encountered the above errors in [email protected]+ including the latest beta 16. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
PS: I know creating an issue is easier than fixing it. I'm quite familiar with JS/TS, if anyone can point me in the right direction, I can try fixing it. |
Beta Was this translation helpful? Give feedback.
-
You need to cast 'child' in the iterate handler or TS won't understand what it is. |
Beta Was this translation helpful? Give feedback.
-
Cast 'child' to what type? |
Beta Was this translation helpful? Give feedback.
-
To what it is - an |
Beta Was this translation helpful? Give feedback.
-
Here is a proper solution to this issue: const stars = this.physics.add.group({
key: "star",
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 },
});
stars.children.iterate((child) => {
if (!(child instanceof Phaser.Physics.Arcade.Sprite)) {
throw Error("Child is not an instance of Phaser.Physics.Arcade.Sprite");
}
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
return true;
}); No dirty casting, proper type handling. Phaser should really be more Typescript friendly. |
Beta Was this translation helpful? Give feedback.
To what it is - an
Phaser.Physics.Arcade.Sprite
.