leaf37 Posted February 18, 2016 Share Posted February 18, 2016 Hi :-D Does JS has a function just like a function called getDefinitionByName in Flash as3? Link to comment Share on other sites More sharing options...
Zeterain Posted February 18, 2016 Share Posted February 18, 2016 An example of getDefinitionByName in AS3 from here : var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class; var instance:Object = new ClassReference(); This uses getDefinitionByName to eventually create a new Sprite. To my knowledge, JS doesn't have a function that will find another fully qualified function by a string, like in the example. I can think of some creative ways to try and fake it, but everything would just end up being hacky. In JS, and particularly with Phaser, creating a new Sprite object would look like: var player = new Phaser.Sprite(...); Or, if you already had a reference to a game object: var player = game.add.sprite(...); You would add the necessary parameters instead of the ellipsis, of course. http://phaser.io/docs/2.4.4/Phaser.Sprite.html TL;DR: Nope. Link to comment Share on other sites More sharing options...
leaf37 Posted February 18, 2016 Author Share Posted February 18, 2016 4 minutes ago, Zeterain said: An example of getDefinitionByName in AS3 from here : var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class; var instance:Object = new ClassReference(); This uses getDefinitionByName to eventually create a new Sprite. To my knowledge, JS doesn't have a function that will find another fully qualified function by a string, like in the example. I can think of some creative ways to try and fake it, but everything would just end up being hacky. In JS, and particularly with Phaser, creating a new Sprite object would look like: var player = new Phaser.Sprite(...); Or, if you already had a reference to a game object: var player = game.add.sprite(...); You would add the necessary parameters instead of the ellipsis, of course. http://phaser.io/docs/2.4.4/Phaser.Sprite.html TL;DR: Nope. Thank you~ It's really a pity..for I just make many class extends Phaser.Sprite like Monster1 Monster2 Monster3 .... I think that if there is a function like getDefinitionByName. I can just add some custom properties in Tiled of a object just like a property called MonsterName and its value is Monster1 of a box called box1 So if the player in my game collide box1 it could automatically create a Monster1. But my current code is like this .. I think it's miserable :-D var tempMonster = new Monster1(obj2.x,obj2.y -20); tempMonster.body.velocity.y = -100; this.groupMonster.add(tempMonster); Link to comment Share on other sites More sharing options...
Zeterain Posted February 18, 2016 Share Posted February 18, 2016 Haha, it might be different than the AS3 ways, but I suppose this is just one of the differences you'll have to get used to Best of luck! Link to comment Share on other sites More sharing options...
drhayes Posted February 18, 2016 Share Posted February 18, 2016 There's no "native" way to do this, but you could set it up yourself. function Monster1Class() {} function Monster2Class() {} var monsterClasses = { monster1: Monster1Class, monster2: Monster2Class }; var monster = new monsterClasses['monster1'](); That's a very abbreviated version, but that's how I'd map strings to functions (and constructor functions) in JS. leaf37 1 Link to comment Share on other sites More sharing options...
leaf37 Posted February 19, 2016 Author Share Posted February 19, 2016 23 hours ago, leaf37 said: Hi :-D Does JS has a function just like a function called getDefinitionByName in Flash as3? Thank you. Besides I have to define every class in a map.It works well~ :-D Link to comment Share on other sites More sharing options...
leaf37 Posted February 19, 2016 Author Share Posted February 19, 2016 10 hours ago, drhayes said: There's no "native" way to do this, but you could set it up yourself. function Monster1Class() {} function Monster2Class() {} var monsterClasses = { monster1: Monster1Class, monster2: Monster2Class }; var monster = new monsterClasses['monster1'](); That's a very abbreviated version, but that's how I'd map strings to functions (and constructor functions) in JS. Thank you. Besides I have to define every class in a map.It works well~ :-D Link to comment Share on other sites More sharing options...
drhayes Posted February 19, 2016 Share Posted February 19, 2016 There are ways to do this "automatically". Depending on how you structure your game, if you do anything like "namespacing" where you do this: window.enemies = window.enemies || {}; window.enemies.Monster1 = function() {} window.enemies.Monster2 = function() {} Then you've got enemies as your map. At least you didn't duplicate anything that way. Link to comment Share on other sites More sharing options...
Recommended Posts