Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by TigerJ

I am attempting my first plugin for a semi dynamic menu

I have this method defined below:
addItem: function(menutext, menulabel, inputAction,inputEvent){
            var item=new Object;
            item.text = menutext;
            item.label = menulabel;
            item.inputAction = inputAction;
            item.inputEvent = inputEvent;
            this.MenuItems.push( item );
        },

here is an example of it being called:
this.myMenu.addItem("polaris","cat","shoot",{
            action: function(){
                console.log("black cat");
            }
        });

I had planned on the following to establish each items event/action during the update inside my plugin file.

for(i=0;i<this.MenuItems.length;i++)
            {
                //establish bounds
                var leftBounds = this.MenuStart.x+((this.ButtonSize.x+this.Spacing.x)*(i));
                var rightBounds = this.MenuStart.x+((this.ButtonSize.x+this.Spacing.x)*(i))+this.ButtonSize.x;
                var upperBounds = this.MenuStart.y+((this.ButtonSize.y+this.Spacing.y)*(i));
                var lowerBounds = this.MenuStart.y+((this.ButtonSize.y+this.Spacing.y)*(i))+this.ButtonSize.y;
                //listen for specified input event actions and if actions while in bounds run functions
                if(ig.input.mouse.x>leftBounds&&ig.input.mouse.x<rightBounds&&ig.input.mouse.y>upperBounds&&ig.input.mouse.y<lowerBounds)
                {
                    if(ig.input.pressed(this.MenuItems[i].inputAction))
                    {
                       this.MenuItems[i].inputEvent.action();
                    }
                }                
            }

What I am noticing however is that the first menu item properly functions, and any other item does not behave as planned (the additional items added replicate the first items inputAction and inputEvent.

Any advice would be great. I've written tons of javascript but this is my first plugin attempt. :)

You can view a baked version of this in action here: http://devbriggs.com/experiment/index.html
(open console click polaris or violett. both perform the inputEvent for polaris)

1 decade ago by TigerJ

I believe the real issue here is I was looping over an array of objects within a classes update method rather than creating a new class and update method and associate an array of these class items rather than an array of objects.

I think creating a menuItem class and using that instead of a generic object will fix this.

Thanks for the reads.

EDIT:
It's working now I looked at how Dominic was handling the game.js and entities updates and copied his style :) thanks for such legible source Sir.

1 decade ago by TigerJ

https://github.com/TigerJ/DarcyMenu

https://github.com/TigerJ/DarcyMenu/wiki

Code for my plugin is up on github with an example main.js

it's a WIP with big plans :) enjoy

1 decade ago by fulvio

This is brilliant, thanks TigerJ.
Page 1 of 1
« first « previous next › last »