Jump to content

Change button frame by name?


proyb2
 Share

Recommended Posts

Nope, to get sprite unique name from stage and set frame. I could only find getAt().frame=1 but don't see any api to getName().frame=1? Just wonder, is there a more efficient way to set frame without looping through all object every few millseconds?

Link to comment
Share on other sites

I still don't quite understand what you need but I'm going to throw some info anyways, please tell me if i'm getting closer  :P

 

Both the Sprite and the Button class have key parameters, so if you do:

function preload() {    game.load.image('sky', 'http://i.imgur.com/826zH6I.png');}function create() {    var bg = game.add.sprite(0, 0, 'sky');    console.log(bg.key);}

The result should be "sky".

 

Then you can do:

game.add.button(0, 0, bg.key);

And you would get a button with the same image as an sprite, If you are going to test this, please don't use the sky image, use something smaller :)

 

Now, if you load a spritesheet instead of an image you can do:

// The nulls are for callback and callback contextgame.add.button(0, 0, anim_spritesheet.key, null, null, 1, 2, 3, 4);

This will allow you to access the frames.

 

 

Hope it helps!

Link to comment
Share on other sites

I still don't quite understand what you need but I'm going to throw some info anyways, please tell me if i'm getting closer  :P

 

Both the Sprite and the Button class have key parameters, so if you do:

function preload() {    game.load.image('sky', 'http://i.imgur.com/826zH6I.png');}function create() {    var bg = game.add.sprite(0, 0, 'sky');    console.log(bg.key);}

The result should be "sky".

 

Then you can do:

game.add.button(0, 0, bg.key);

And you would get a button with the same image as an sprite, If you are going to test this, please don't use the sky image, use something smaller :)

 

Now, if you load a spritesheet instead of an image you can do:

// The nulls are for callback and callback contextgame.add.button(0, 0, anim_spritesheet.key, null, null, 1, 2, 3, 4);

This will allow you to access the frames.

 

 

Hope it helps!

 

 

Sorry, the code you mentioned have been done before. To clarify, say I created a set of piano keyboards of 88 keys from C0 to C7:

keyboard = game.add.group();item = keyboard.create(i, 'wkey', 0)item.input.start(0, true)item.events.onInputDown.add(kpress)item.events.onInputUp.add(krelease)item.events.onInputOut.add(kmoveoff)item.name="C0"
 
and each items is added into the stage, I could change the frame to depress key in onInputDown event thru my mouse, in other method, how do you depress "C0" or other keys if Phaser received an input from MIDI events like "C0" and Phaser will lookup for an items with that name and depress or change frame to 1?
Link to comment
Share on other sites

Try creating the keys inside an array, something like keys[index] = keyboard.create...

Then, since you know the order of the keys you could say that index 0 is key C0 (or whatever, I have no knowledge of any musical instrument) :P

You could even use an object:

var obj = {    "C0": 0,    "C7": 7}keys[ obj["C0"] ]; // it's the same as doing keys[0] and would return the button saved at index 0 of the array.

With this you can make a function that takes a parameter of type string (the key name), gets the correct button and changes its frame.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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