Jump to content

Menu keys callback usage


Recommended Posts

I'm making a menu and decided to use onDownCallback. The options are text objects(game.add.text(....)). The selected option should become active. But it works only once and for the first chosen option - up or down. Then nothing happens. BUT the debugger goes in the called functions everytime I press 'up' or 'down'.

create() {  down.onDown.add(optionB, this);  up.onDown.add(optionA, this);}function optionA() {    optionAText.fill = activeFill;}    function optionB() {    optionBText.fill = activeFill;}

Edit: actually, everything works but the text color(fill) does not update.


Another thing I tried:

update() {           if (this.game.input.keyboard.lastKey == down) {            optionA.fill = deactiveFill;            optionB.fill = activeFill;        } else if (this.game.input.keyboard.lastKey == up) {            optionA.fill = activeFill;            optionB.fill = deactiveFill;        }}

I don't know why but this makes both options change color. If I press 'down', both are activeColor, if I press 'up', both get deactiveColor.
... Great! I have other ideas how to do what I want but I'd like to know what's wrong with the upper 2.

Link to comment
Share on other sites

It is.

Damn! It was the style that messed things up. I should've mentioned I was using such. Who would've thought.

 activeFill = "#093"; deactiveFill = "#000"; style = { font: "bold 25px Forte", fill: deactiveFill}; optionA= game.add.text(0, 0, 'optionA', style); optionB= game.add.text(10, 10, 'optionB', style);

But this works:

optionAText = game.add.text(0,0, 'Play', {fill: deactiveFill});optionBText = game.add.text(10,10, 'Instructions', {fill: deactiveFill});optionAText.cssFont  = "bold 25px Forte";optionBText.cssFont  = "bold 25px Forte";

Thanks, Trissolo! :)
Now which post should I mark for an answer?

Link to comment
Share on other sites

 your style object is ok: you can declare in a row all properties you need (e.g. font family and color. No need to assign cssFont later).
 Just remember that Text.fill receives a string.
 activeFill = "#093" // no problems
 activeFill = {fill: "#093"} // wrong
 So, starting with your code, this example works: :)

function create() {        activeFill = "#093"; //Ok    //activeFill = {fill: "#093"}; // No        deactiveFill = "#31B";    style = { font: "bold 25px Arial Black", fill: deactiveFill, align: "left", wordWrap: true, wordWrapWidth: 300}; // Just an example            optionAText = game.add.text(22,22, "Play", style);    optionBText = game.add.text(22,66, "Instructions",  { font: "25px Impact", fill: deactiveFill}); // another style, just for fun        down  = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);    up  = game.input.keyboard.addKey(Phaser.Keyboard.UP);        down.onDown.add(optionB, this);    up.onDown.add(optionA, this);}function optionA() {    optionAText.fill = activeFill;    optionBText.fill = deactiveFill;}  function optionB() {    optionAText.fill = deactiveFill;    optionBText.fill = activeFill;}



(Darn, my english still s***) :o

Link to comment
Share on other sites

I see. Try this:

optionAText = game.add.text(22,22, "Play", style);optionBText = game.add.text(22,66, "Instructions",  style); // another style, just for fun



That's the problem I had initially. By setting the fill in style and then passing the style to the text, things get messed up.


And your English is fine. :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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