ManBoy Posted March 12, 2014 Share Posted March 12, 2014 Hi, how do I cast in JavaScript?function ballHitBrick(ball, brick){ console.log(Phaser.Group(brick.parent)); console.log(brick.parent as Phaser.Group);}I tried the code above and this error came out: Uncaught TypeError: Cannot call method 'addChild' of undefined phaser.min.js:5 c.Groupphaser.min.js:5 ballHitBrickBreakout.js:164 c.Physics.Arcade.collideSpriteVsGroupphaser.min.js:12 c.Physics.Arcade.collideHandlerphaser.min.js:12 c.Physics.Arcade.collidephaser.min.js:12 updateBreakout.js:112 c.StateManager.updatephaser.min.js:4 c.Game.updatephaser.min.js:5 c.RequestAnimationFrame.updateRAFphaser.min.js:8 window.requestAnimationFrame._onLoopphaser.min.js:8 Link to comment Share on other sites More sharing options...
StrykerKKD Posted March 12, 2014 Share Posted March 12, 2014 There is no casting in Javascript. Why do you want to cast? You get an undefined error because you call the Phaser.Group() function with invalid arguments.http://docs.phaser.io/Phaser.Group.html#toc2 You should start learning Javascript, if you don't know this. Link to comment Share on other sites More sharing options...
ManBoy Posted March 12, 2014 Author Share Posted March 12, 2014 I need to get some public variables which I defined in my custom class that extends group.Yes, I'm new to JavaScript and HTML5 Games Dev in general. Link to comment Share on other sites More sharing options...
StrykerKKD Posted March 12, 2014 Share Posted March 12, 2014 There is a Phaser example for this:http://examples.phaser.io/_site/view_full.html?d=groups&f=extending+a+group.js&t=extending%20a%20group Link to comment Share on other sites More sharing options...
ManBoy Posted March 12, 2014 Author Share Posted March 12, 2014 I already created a custom class. What I need is for the collider in the event, in this case, the brick.parent to recognize it as Brick (extending Phaser.Group that contains separate sprites e.g:eyes, mouth etc), so it can retrieve a public variable I defined it in. In case of the example you've given, I need something like this:MonsterGroup = function (game, image, action) { Phaser.Group.call(this, game); this.hobby = 'algebra';}function playerHitMonster(player,monster){ console.log(monster.parent.hobby); //console.log((monster.parent as MonsterGroup).hobby);}Since I can't cast in JavaScript, I'm kinda lost how to recognize the collider's parent as MonsterGroup to retrieve its hobby. Link to comment Share on other sites More sharing options...
r00 Posted March 12, 2014 Share Posted March 12, 2014 You can just check if the object contains the field you are looking for, and then expect the object to be the type you expect it to be:if (monster.parent.hobby) { // given that you trust monster to always have a parent property to begin with console.log(monster.parent.hobby);}It's probably not the cleanest way, but it should do the trick as long as you are not storing booleans in the property that you are testing. Link to comment Share on other sites More sharing options...
Recommended Posts