Jump to content

Quesion about a JS function


leaf37
 Share

Recommended Posts

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

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

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.

Link to comment
Share on other sites

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...