DNESS / cocos2d-iphone

Automatically exported from code.google.com/p/cocos2d-iphone
1 stars 0 forks source link

MenuItemSprite doesn't draw children of the sprite #458

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hey Guys,

Could someone please tell me why this is not working ?

Sprite *mySprite = [Sprite spriteWithFile:@"button-plain1.png"];
Label *mySpriteLabel = [Label labelWithString:@"Start Game"
fontName:@"Zapfino" fontSize:18]; //creates label for sprite
[mySpriteLabel setRGB:255 :255 :255]; //sets color of label
[mySprite addChild: mySpriteLabel z:1]; // adds label to sprite
CGSize s = [mySpriteLabel contentSize];
mySpriteLabel.position = cpv(s.width * 0.50, s.height * 0.50); // positions
label on sprite (cpv is relative to 0,0 point on parent sprite)
From http://www.cocos2d-iphone.org/forum/topic/832:

MenuItemSprite *item1 = [MenuItemSprite itemFromNormalSprite: mySprite
selectedSprite: mySprite disabledSprite: mySprite target:self
selector:@selector(startGame:)];

Menu *menu = [Menu menuWithItems:item1, nil];
[menu alignItemsVertically];
[self addChild: menu z:2];
menu.position = cpv(320/2,100);

The problem is I see only the button and not the text in it. However if I
add mySprite to the Layer, I can see both the button and text on it.

I really appreciate all your help. 

Original issue reported on code.google.com by ricardoq...@gmail.com on 22 Jul 2009 at 4:24

GoogleCodeExporter commented 9 years ago
I had the same problem, so I changed MenuItemSprite to properly draw the 
children.
Attached is the patch (based off latest trunk r1307).

Original comment by myBuddyCJ@gmail.com on 25 Aug 2009 at 4:57

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 27 Aug 2009 at 1:17

GoogleCodeExporter commented 9 years ago
to be addressed with the new sprite refactoring in v0.9

Original comment by ricardoq...@gmail.com on 5 Oct 2009 at 6:48

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 8 Feb 2010 at 4:21

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 8 Feb 2010 at 4:24

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 19 Mar 2010 at 10:48

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 24 Apr 2010 at 2:12

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 26 May 2010 at 6:20

GoogleCodeExporter commented 9 years ago

Original comment by ricardoq...@gmail.com on 14 Jul 2010 at 9:48

GoogleCodeExporter commented 9 years ago
Issue 990 has been merged into this issue.

Original comment by ricardoq...@gmail.com on 24 Oct 2010 at 3:34

GoogleCodeExporter commented 9 years ago
fixed
http://github.com/cocos2d/cocos2d-iphone/commit/efdcb3ac13ca86f9161b825ab45cf626
fc0be7c2

Original comment by ricardoq...@gmail.com on 2 Nov 2010 at 7:36

GoogleCodeExporter commented 9 years ago
Still not working complete (at least not for me).

The children are being drawn fine now however there's no way to click the 
button (even the selected: item doesn't work).

In my case, I subclassed CCSprite and added a few elements (CCColorLayer, some 
CCSprites and a CCLabel). The CCSprite itself doesn't have any images or 
anything - just children.

Original comment by nedraf...@gmail.com on 3 Nov 2010 at 1:52

GoogleCodeExporter commented 9 years ago
@nedrafehi: Could you attach a test case ? Thanks.

reopened.

Original comment by ricardoq...@gmail.com on 3 Nov 2010 at 3:00

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Here is the actual test code (just copy into a black scene) and it should work. 
The actual class and images used are attached.

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init] )) {

        // ask director the the window size
        CGSize size = [[CCDirector sharedDirector] winSize];

        QuestionContainerSprite* question = [[QuestionContainerSprite alloc] init];
        QuestionContainerSprite* question2 = [[QuestionContainerSprite alloc] init];

        CCMenuItemSprite* sprite = [CCMenuItemSprite itemFromNormalSprite:question2 selectedSprite:question target:self selector:@selector(selectAnswer:)];

        CCColorLayer* layer = [CCColorLayer layerWithColor:ccc4(0,0,255,255) width:100 height:100];
        CCColorLayer* layer2 = [CCColorLayer layerWithColor:ccc4(255,0,0,255) width:100 height:100];

        CCMenuItemSprite* sprite2 = [CCMenuItemSprite itemFromNormalSprite:layer selectedSprite:layer2 target:self selector:@selector(selectAnswer:)];        
        CCMenu* menu = [CCMenu menuWithItems:sprite, sprite2, nil];
        [menu alignItemsVerticallyWithPadding:100];

        [menu setPosition:ccp(size.width / 2, size.height / 2)];

        // add the label as a child to this Layer
        [self addChild: menu];
    }
    return self;
}

-(void)selectAnswer:(id)sender
{
    CCLOG(@"Selected");
}

Original comment by nedraf...@gmail.com on 3 Nov 2010 at 4:23

Attachments:

GoogleCodeExporter commented 9 years ago
workaournd:
Set the contentoSize of QuestionContainerSprite before adding it into the 
MenuItem.

eg:
[question setContentSize: size_of_children];

I'll mark this bug as fixed, since the original bug is fixed.
Please, open a new bug if you want that the menu automatically tests 
"collision" with its children.

Original comment by ricardoq...@gmail.com on 3 Nov 2010 at 8:19

GoogleCodeExporter commented 9 years ago
Benihana test code does not work before and after the fix for different reasons:

CCSprite *tempSprite = [CCSprite spriteWithFile:@"Icon.png"];
[tempSprite addChild:[CCSprite spriteWithFile:@"fps_images.png"] z:1];

CCMenuItem *thisMenuItem = [CCMenuItemSprite itemFromNormalSprite:tempSprite 
selectedSprite:tempSprite]; // SIGABORT

CCMenu *thisMenu = [CCMenu menuWithItems:thisMenuItem, nil];
[self addChild:thisMenu];

But this one is ok:

CCSprite *tempSprite = [CCSprite spriteWithFile:@"Icon.png"];
[tempSprite addChild:[CCSprite spriteWithFile:@"fps_images.png"] z:1];

CCSprite *tempSprite1 = [CCSprite spriteWithFile:@"Icon.png"];
[tempSprite1 addChild:[CCSprite spriteWithFile:@"fps_images.png"] z:1];

CCMenuItem *thisMenuItem = [CCMenuItemSprite itemFromNormalSprite:tempSprite 
selectedSprite:tempSprite1]; // ok

CCMenu *thisMenu = [CCMenu menuWithItems:thisMenuItem, nil];
[self addChild:thisMenu];

Original comment by a1rex2...@gmail.com on 5 Nov 2010 at 12:29