Forum Libraries, Components, and Plugins

CCMenuImageItem does not trigger action

I am learning to build menus using CCMenu and related classes. I am modelling my attempt after the Nibbles game by James Montemagno. (https://github.com/jamesmontemagno/nibbles)

This is my GameStartLayer class.

public class GameStartLayer : CCLayerColor
{
    CCSprite rtLogo, title, menuStart;
    CCRepeatForever repeatedAction;
    CCLabel menuTest;

    public GameStartLayer () : base (new CCColor4B(0,0,0))
    {
        Color = new CCColor3B(0,0,0);
        Opacity = 255;

        // Define Actions
        var moveUp = new CCMoveBy (1.0f, new CCPoint(0.0f, 50.0f));
        var moveDown = moveUp.Reverse ();

        //Define Sequence of actions
        var moveSeq = new CCSequence (new CCEaseBackInOut (moveUp), new CCEaseBackInOut (moveDown));

        repeatedAction = new CCRepeatForever (moveSeq);
    }

    void StartGame (object stuff = null)
    {
        var mainGame = GameLayer.CreateScene (Window);
        var transitionToGame = new CCTransitionMoveInR (0.3f, mainGame);
        Director.ReplaceScene (transitionToGame);
    }

    protected override void AddedToScene ()
    {
        base.AddedToScene ();

        var textColor = CCColor3B.White;

        // Use the bounds to layout the positioning of our drawable assets
        CCRect bounds = VisibleBoundsWorldspace;

        menuStart = new CCSprite ("bb_startgame");
        menuStart.AnchorPoint = CCPoint.AnchorMiddle;

        menuTest = new CCLabel ("Test", "arial", 48, CCLabelFormat.SystemFont) {
            Color = textColor,
            HorizontalAlignment = CCTextAlignment.Right,
            VerticalAlignment = CCVerticalTextAlignment.Center,
            AnchorPoint = CCPoint.AnchorMiddle
        };

        var menuItemStart = new CCMenuItemImage (menuStart, StartGame);  //Doesn't trigger the StartGame method
        var menuItemTest = new CCMenuItemLabel (menuTest, StartGame); // Does trigger the StartGame method

        var menu = new CCMenu (menuItemStart, menuItemTest) {
            Position = new CCPoint (bounds.Size.Width / 2, bounds.Size.Height / 2),
            AnchorPoint = CCPoint.AnchorMiddle
        };

        menu.AlignItemsVertically (50);

        AddChild (menu);

        rtLogo = new CCSprite ("bb_retrotek");
        rtLogo.AnchorPoint = CCPoint.AnchorMiddle;
        rtLogo.Position = new CCPoint (bounds.Size.Width / 2, bounds.Size.Height / 14);
        //rtLogo.RunAction (repeatedAction);
        AddChild (rtLogo);

        title = new CCSprite ("bb_title");
        title.AnchorPoint = CCPoint.AnchorMiddle;
        title.Position = new CCPoint (bounds.Size.Width / 2, (bounds.Size.Height / 4)*3);
        title.RunAction (repeatedAction);
        AddChild (title);
    }

    public static CCScene CreateScene (CCWindow mainWindow)
    {
        var scene = new CCScene (mainWindow);
        var layer = new GameStartLayer ();

        scene.AddChild (layer);

        return scene;
    }
}

I have both a CCMenuItemImage and a CCMenuItemLabel that are doing the same thing, calling the StartGame method. However only the CCMenuItemLabel works. The CCMenuItemImage object lets me click on it but the screen doesn't transition. Is this a bug or does the CCMenuItemImage need to be set up differently?

Answers

  • JaredMathesJaredMathes CAUniversity

    The problem was this line:

        var menuItemStart = new CCMenuItemImage (menuStart, StartGame);  
    

    For some reason, this acts different. I noticed the overload with 2 arguments is as follows:

    public CCMenuItemImage (CCSprite normalSprite, Action<object> selector = null)
    

    The overload with 3 arguments is:

    public CCMenuItemImage (CCSprite normalSprite, CCSprite selectedSprite, Action<object> target = null)
    

    I'm not sure why, but I think the change from 'selector' to 'target' is what is changing the behavior. I have no idea if this is intended or not. If it is, I'm interested what the difference means (if anyone knows).

Sign In or Register to comment.