CCMenuAdvanced menus that scroll together

Working on the interface for buying and selling items in Dungeonism, I needed a menu that was arranged as a grid. There seem to be some choices out there for this, but I couldn’t get anything to drop simply into my project. Instead, I came up with my own solution, which is a clunky, but at least it’s simple.

I’ve been using the CCMenuAdvanced cocos2d extension to my single-column scrolling menus, so I made a CCNode subclass to which you can attach several menus. In its draw function, it checks to see if any of the menus have moved up or down, and if it finds any, it moves all menus in the same way. The code for the class is below.

Also, I use this ClippingNode class for the menus to scroll inside.

//  ScrollingMenuSet.h

#import 
#import "cocos2d.h"

@interface ScrollingMenuSet : CCNode {
    float scroll;
}

@end
//  ScrollingMenuSet.m

#import "ScrollingMenuSet.h"

@implementation ScrollingMenuSet

- (id)init
{
    if (self = [super init]) {
        scroll = 0;
    }
    return self;
}

- (void)draw
{
    float newScroll = scroll;
    for (CCNode *child in self.children) {
        if (child.position.y != scroll) {
            newScroll = child.position.y;
        }
    }
    scroll = newScroll;
    for (CCNode *child in self.children) {
        if (child.position.y != scroll) {
            child.position = ccp(child.position.x, scroll);
        }
    }
    [super draw];
}

@end
Tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>