Jump to content

Information on making instances instead of using game.add


d0nkeykong
 Share

Recommended Posts

Hi all.

 

I'm new to Phaser and although I appreciate the game.add methods, I cannot find any information on making instances and adding them to my game.

Can anyone point me in the right direction? 

 

Using the sound class as an example, I have tried the following but it does not work:

bgMusic = new Phaser.Sound(game, 'intro-music', 1, true);bgMusic.play();

But this does

bgMusic = game.add.audio('intro-music');bgMusic.play();

Also the fadeOut method does not work on the latter for some reason?

 

Thanks in advance.

Link to comment
Share on other sites

Audio should be created via the SoundManager.

 

game.sound.add would create a new Sound object, add it to th SoundManager and return a reference to it.

 

If you absolutely must create it outside of the SoundManager then it needs adding to the SoundManagers private _sounds array before it can be played:

game.sound._sounds.push(bgMusic);

Otherwise it won't get updated by the main loop, be able to pause, fade, etc.

 

The GameObjectFactory (game.add) often does a lot more than just simple object creation. While it's possible to circumvent it entirely there's no good reason I can think of to do so.

Link to comment
Share on other sites

Thanks for the reply rich.

 

It makes sense when it is explained so I will continue to use the add methods. It just takes some getting used to :)

In other frameworks, I am used to making an instance, setting properties and then adding it to the canvas / stage etc.

 

I cannot get the Sound.fade methods to work but I have added another post to cover this.

Link to comment
Share on other sites

Yeah I can see how it's different to other frameworks. But if I can do it in a single call, I will :)

 

BTW the difference between game.add.sprite (or add.whatever) is that it adds it to the display list immediately. If you just want to create something for use later on then game.make.sprite is your friend.

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...