cocos2d Move Sprite using buttons - ios

Using CCMenu I have create Two Buttons Up and Down Here is the code
CCSprite *normlUp = [CCSprite spriteWithFile:#"Up.png"];
CCSprite *selectedUp = [CCSprite spriteWithFile:#"Up.png"];
selectedUp.color = ccGREEN;
CCMenuItemSprite *up = [CCMenuItemSprite itemFromNormalSprite:normlUp selectedSprite:selectedUp target:self selector:#selector(upItemTouched)];
up.position = CGPointMake(-220, -115);
CCSprite *normlDown = [CCSprite spriteWithFile:#"Down.jpeg"];
CCSprite *selectedDown = [CCSprite spriteWithFile:#"Down.jpeg"];
selectedDown.color = ccGREEN;
CCMenuItemSprite *down = [CCMenuItemSprite itemFromNormalSprite:normlDown selectedSprite:selectedDown target:self selector:#selector(downItemTouched)];
down.position = CGPointMake(-220,-140 );
CCMenu *upDown = [CCMenu menuWithItems:up,down,nil];
[self addChild:upDown z:4];
How to write upItemTouched and downItemTouched Methods
Also the sprite should move smoothly on the screen
I am New to cocos2d so please accept my simple questions........

Use CCMenuItemSprite assigning adding selector to the sprite, so when you touch down/up sprite that selector method will be called.
so, when "up" is touched, set one boolean to true and when "down" is touched, set another boolean to true.
Now in update/tick method check which boolean in true and then move the sprite.
When done moving, in touchesEnded method just set those boolean to false.

Related

Get position of CCSprite

Im a bit confused. I am trying to move one CCSprite onto the position of another CCSprite. I figured it would be simple, but the CCSprite isn't positioning correctly. I have 2 CCSprites:
//Get Position of EMPTY SPRITE
CCSprite *sprite = (CCSprite*)[self getChildByName:[NSString stringWithFormat:#"%d", kEMPTY] recursively:NO];
//GET DRAGGED SQUARE
CCSprite *spriteMove = (CCSprite*)[self getChildByName:[NSString stringWithFormat:#"%d", DragStartSquare] recursively:NO];
I am then trying to get the position of the EMPTY SPRITE (Its not actually empty) by doing
CGPoint worldCoord = [sprite convertToWorldSpace: sprite.position];
I am then animating it with:
id action1 = [CCActionMoveTo actionWithDuration:duration position:worldCoord];
This sends the sprite off the screen. What am I doing wrong?
You need to convert back into the target sprite's node space:
CGPoint targetNodeCoord = [spriteMove convertToNodeSpace: [sprite convertToWorldSpace: sprite.position]];
CCAction *action1 = [CCActionMoveTo actionWithDuration:duration position:targetNodeCoord];

Cocos2D: how to tweak CCMenuItem with only one sprite?

how could I tweak CCMenuItemSprite to support only one sprite?
Currently I have:
[CCMenuItemSprite itemWithNormalSprite:one selectedSprite:selectedOne]
But would like to have:
[CCMenuItemSprite itemWithNormalSprite:one]
EDIT: I want to modify CCMenuItem to work only with one CCSprite and not two. So I need to change also the internal methods.
You can just use the same (normal) sprite as the selected sprite. When clicked, the button will then do nothing.
You could just use
[CCMenuItemSprite itemWithNormalSprite:one selectedSprite:one]
this way, nothing would happen when you select the sprite
try this, just change colour of seleted sprite.
CCSprite *sprite1 = [CCSprite spriteWithFile:#"Button.png"];
CCSprite * sprite2 = [CCSprite spriteWithFile:#"Button.png"];
sprite2.color = ccc3(128, 128, 128);
CCMenuItemImage *itemEasyLevelImage = [CCMenuItemImage itemWithNormalSprite:sprite1
selectedSprite:sprite2
block:^(id sender){}];

CCMoveTo not working, node/scene issue?

I have a CCLayer class called SuccessLayer. It gets added to the scene when the level is complete, like so:
SuccessLayer *successLayer = [SuccessLayer node];
[self addChild:successLayer];
In SuccessLayer, I want to have a rock fly by, I'm trying to achieve that with this:
-(void)onEnter{
Asteroid *asteroid = [Asteroid spriteWithFile:#"rocks.png"];
asteroid.position = ccp(0, 500);
[self addChild:asteroid];
CCMoveTo *move = [CCMoveTo actionWithDuration:2.0 position:ccp(1000, 0)];
[asteroid runAction:move];}
However, it seems CCMoveTo isn't working. I see the sprite sitting at its initial coordinates, but nothing more. What am I missing here? Thanks
[super onEnter];
any coco's onSomething, you should super onSomething.
Sovled the problem by casting it as a CCSprite (is that the correct way to say it?)
CCSprite *asteroid = [Asteroid spriteWithFile:#"rocks.png"];
Asteroid is already a subclass of CCSprite, so I have no idea why this works, but it allows me to run actions on it now.

Scaling CCMenuItemSprite items in a CCMenu

I'm having problems using CCMenu with scaled CCMenuItemSprite menu items. I'm trying to scale the menu item sprites differently based on which device the game is being played on (the iPad needs to scale it to about 1.5x, whereas on the iPhone it's about 0.75x)
From what I've read, we can't scale the CCSprite directly, or the CCMenuItemSprite, because when it's added to the CCMenu the touch rectangles aren't updated correctly. I believe that I have to scale the CCMenu to scale the Menu Items.
Whenever I do this my sprites appear to be scaled to the correct sizes, however it also seems to scale the CCMenu position coordinates, but in the opposite direction as what I'd expect. Also once I go over a certain threshold the menu seems to disappear altogether.
Does anyone have any suggestions on how I should be scaling Sprites in a CCMenu?
Thanks in advance.
Buzzrick
Try this Code........
CCMenuItemImage *Btn1 = [CCMenuItemImage itemWithNormalImage:#"button1.png" selectedImage:#"button1_active.png" target:self selector:#selector(button1_click:)];
CCMenuItemImage *Btn2 = [CCMenuItemImage itemWithNormalImage:#"button2.png" selectedImage:#"button2_active.png" target:self selector:#selector(button2_click:)];
CCMenu *Action_menu = [CCMenu menuWithItems:Btn1,Btn2, nil];
[Action_menu setPosition:ccp( 79, 288)];
float delayTime = 0.3f;
for (CCMenuItemFont *each in [Action_menu children])
{
each.scaleX = 0.0f;
each.scaleY = 0.0f;
CCAction *action = [CCSequence actions:
[CCDelayTime actionWithDuration: delayTime],
[CCScaleTo actionWithDuration:0.5F scale:1.0],
nil];
delayTime += 0.2f;
[each runAction: action];
}
[self addChild:Action_menu];
My suggestion? Don't!
Internally CCMenu scaled the menu items whenever you touch them. You'll notice this if you tap and hold an item, it's scaled up (zoomed in). So whatever scaling you apply to menu items is lost at the latest when the menu item gets touched.
And then, as you noticed, scaling affects the touch area of an item. It can lead to items responding to touches outside the item, or not responding to touches depending on zoom level. I also wouldn't scale the CCMenu for the same reasons.
Long story short, if you have to apply scaling to your menu items, write your own menu item code. Possibly by basing it on the CCMenu code and stripping out what you don't need and making your desired changes.
But really the easiest way is to supply the menu item images using the file suffixes -hd, -ipad and -ipadhd and scaled correspondingly. Trying to get this right with the scale property is just painful.
Here's how I resolved it in the end:
Basically I created the menu elements first, THEN I sorted out the positioning/scaling/rotation. This seems to work much better.
This code sample below is where i'm using a single sprite to create two opposing left/right arrow buttons
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"ButtonSprites.plist"];
CCSpriteBatchNode *buttonSprites = [CCSpriteBatchNode batchNodeWithFile:#"ButtonSprites.png"];
[self addChild:buttonSprites];
CCSprite *arrowLeftSprite = [CCSprite spriteWithSpriteFrameNameOrFile:#"PageArrow"];
CCSprite *arrowLeftSpriteSelected = [CCSprite spriteWithSpriteFrameNameOrFile:#"PageArrow"];
arrowLeftSpriteSelected.opacity = 128;
CCSprite *arrowRightSprite = [CCSprite spriteWithSpriteFrameNameOrFile:#"PageArrow"];
CCSprite *arrowRightSpriteSelected = [CCSprite spriteWithSpriteFrameNameOrFile:#"PageArrow"];
arrowRightSpriteSelected.opacity = 128;
float buttonWidth = screenSize.width * ButtonWidthPercent;
int contentPixelWidth = arrowLeftSprite.contentSize.width;
float scale = buttonWidth / contentPixelWidth;
CCMenuItemSprite *pageLeftMenu = [CCMenuItemSprite itemFromNormalSprite:arrowLeftSprite selectedSprite:arrowLeftSpriteSelected target:self selector:#selector(buttonPageLeft:)];
CCMenuItemSprite *pageRightMenu = [CCMenuItemSprite itemFromNormalSprite:arrowRightSprite selectedSprite:arrowRightSpriteSelected target:self selector:#selector(buttonPageRight:)];
// First perform the creation
CCMenu *menu = [CCMenu menuWithItems: mainMenu, pageLeftMenu, pageRightMenu, nil];
menu.position = ccp(0, 0);
// THEN deal with layout,rotation, and scaling
pageLeftMenu.scale = scale;
pageLeftMenu.rotation = 180;
pageRightMenu.scale = scale;
float arrowHeight = screenSize.height * 0.2;
pageLeftMenu.position = ccp(screenSize.width * 0.1, arrowHeight);
pageRightMenu.position = ccp(screenSize.width * 0.9, arrowHeight);
[self addChild:menu z:2];

How to implement Sprite with Buttons

So I'm making a game, and in this game I need to have a certain sprite to appear multiple times.
It's simply an image, with two buttons. One button underneath the image, and one above.
I am using Cocos2d, so any mention of sprite will be the CCSprite class. And the image is actually just a line.
Is there an easy way to implement something like this? I don't want to have to make a separate sprite for the image, and then add each button. Is there a way I can do this all in one sprite?
I'm assuming that I'll probably have to manually add everything (create a method that will create an image and position the buttons relative to the image), but I'm hoping I'm wrong and there is an easier/more efficient way of doing this.
Thanks!
I think you are looking for CCMenuItem and CCMenu
CCSprite *enabledSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCSprite *selectedSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCSprite *disabledSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCMenuItemSprite *item [[[CCMenuItem alloc] initWithNormalSprite:enabledSprite
selectedSprite:selectedSprite
disabledSprite:disabledSprite
target:delegate
selector:selector] autorelease];
item.position = ccp(240, 160);
CCMenu *menu = [CCMenu menuWithItems:item, nil];
menu.position = ccp(0, 0);
[self addChild:menu];

Resources