ccTouchesBegan not firing in cocos2D? - cocos2d-iphone

Hello, I have created a layer for a demo game application using cocos2D Framework. I have integrating cocos2d with existing application. I need to detect Touch Events for that. But I am not able to Detect that.I have used ccTouchesBegan.
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self ccTouchesEnded:touches withEvent:event];
}
Even simple touchBegan event is also not fired.
do I have to add anything like delegate or Protocol for that ? I am new in iphone development and also I am learning cocos2d programming for game development . please help.

In the init method of the layer, you need to tell it to respond to touches by setting self.isTouchEnabled = YES;.

Related

Combine Cocos2D (CCAnimation) animation with CocosBuilder (CCBAnimation Manager)?

I'm integrating CocosBuilder 2.1 into an existing Cocos2D-iphone 2.0 game and having some trouble with animations. There are multiple characters in the game that have both repeating sprite-based animations and movement animations, the latter being dynamically based on changing starting and ending positions.
This was easy in pure Cocos2D. I could just write something like:
action = [CCSpawn actions:
[CCRepeat actionWithAction:[CCAnimate actionWithDuration:1.0f animation:flippingAnimation restoreOriginalFrame:YES] times:x],
flippingAction, nil];
However, CocosBuilder uses the CCBANimationManager, which appears to run on an entirely different system. More like:
CCBAnimation Manager *animationManager = self.userObject;
[animationManager runAnimationsForSequenceNamed:#"Flipping"]
I can design the sprite animations in CocosBuilder, but I think my movement animations will have to stay in Cocos2D code. Is there any way to run the two types of animation simultaneously on the same CCSprite?
Thanks for your help.

How can i develop some ViewControllers graphically and then call them from a Cocos2d Iphone 2 scene?

I am new in the cocos2d world.
I am developing a simple game that also has a FORM where several users will fill their contact info (is a game for an event).
The form has to save data in sqlite or in NSUserDefaults array and then when the ipad gets connected to Wifi the user will sync all data to a mySQL using a REST service.
My question is:
Can i start a Cocos2d Project with its template and also add a soryboard or something to develop the non game's part as a usal ViewController?
I saw some posts about using a storyboard and then call the game Scene ass a Segue, but i think it might be better having my main menu and game Scene as Cocos2d Scenes and for the user input call a ViewController that then returns to my Scene (something similar to the Cocos2d iphone 2 project template that calls a ViewController from the Gamekit).
Regards.
MYViewController *vc = [[MYViewController alloc] initWithNibName:#"Myview" bundle:nil];
[[[CCDirector sharedDirector] openGLView] addSubview:vc];
try this code to add view controller

Cocos2D 2.0 different behavior compared to 1.0

I have several apps created with Cocos2D.
I am upgrading some of them to the new release 2.0 of Cocos2D. The method I am using is:
I create a new blank project from the standard template
I move all files to the new project
I generally keep the layer Helloworld as my initial Layer. I have noticed that Cocos2D creates an introLayer as the initial one and I have removed that and reverted the behavior to load Helloworld as before. Said that, I have noticed a few things:
The behavior inside Helloworld's init method is now different. I have noticed that the orientation is now reported incorrectly inside the init method. It will always report it as portrait even if the app is just landscape. What I have noticed also is that it is like the director is not fully initialized yet. This was confirmed if I try to use the director's view. This view will always come as not valid, if I try to use it inside Helloworld's init method.
To solve that, I have moved all code to Helloworld's onEnter method. Now, Helloworld and the director are full initialized and everything works.
This method worked for the first apps that I upgraded for Cocos2D 1.0 to 2.0, but something is wrong for the application I am converting now from Cocos2D 2.0beta to 2.0 final.
The problem I have now is that Helloworld is not triggering any touch and yes, I have this in place:
-(void) registerWithTouchDispatcher
{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
What can be preventing Helloworld from detecting touches in 2.0? How can I make the init method of Helloworld to behave like in 2.0 beta or 1.0?
NOTE: ccTouchBegan and registerWithTouchDispatcher are never called when Helloworld loads.
thanks.
The rotation issue is a known bug. The very first scene will be initialized in portrait mode, even if the app is set to be in landscape only. That's because the init happens before the orientation has been updated by the app delegate. Putting the code in onEnter fixes that, or simply loading an "intro" scene that then loads the actual first scene fixes that as well.
The touch issue is probably not a problem of cocos2d 2.0. Maybe it's as simple as not setting self.isTouchEnabled = YES. If the registerWithTouchDispatcher method isn't called, then isTouchEnabled is not set to YES or you're not doing this in a CCLayer class but maybe CCScene.
The solution for this question is to put [super onEnter] on the onEnter method.
-(void) onEnter {
[super onEnter];
self.isTouchEnabled = YES;
}
the lack of this line was creating all kind of problems on the application, like scheduler not working, weird behavior of ccMove, ccTransition not triggering onEnterTransitionDidFinish and all kind of problems.

cocos2d how do I prevent multi touch propagation to previous layers?

I'm handling the touches manualy through a class that holds every touchable sprite. Also, every menu is a CCLayer, and when I change from one menu to the other, I use the CCTransition### and get the new scene.
So, the layers are "still there" and when I touch one menu, the previous layer also receive it... like when I start the game in a new CCLayer, and then the menus are still affected for the multi touch...
If I use single touch, I can swallow the touches, but the game must have multi touch...
What can I do? Thank you all!
You need to consume the touch event in whatever layer needs it. After a layer consumes it, the touch will no longer be propagated to "previous" layers.
http://www.cocos2d-iphone.org/api-ref/0.9.0-beta/_c_c_touch_delegate_protocol_8h_source.html
To consume a touch event, you return YES in the ccTouchBegan delegate method.
EDIT: changed method name to match CCStandardTouchDelegate
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
if (condition)
return YES;
else
return NO;
}
You can set up the priority to 0 for the top layer and priority to 1 for the layer under it like this.
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
You could try to use the property
self.isTouchEnabled = NO;
within the layer, you don't want to receive touches.
If you you use CCButton objects you need to disable them.
I hope it helps...
Greetings Anselm

How To Retrieve Actions From Sprite In cocos2d

I have a CCSprite that I'm using in a scene and have created multiple CCAnimation actions to apply to it all using a single CCSpriteFrameCache sharedSpriteFrameCache. While everything is working and I'm able to switch between animations, I feel like I'm doing poorly and would like to simplify my code by retrieving the running action(s) on the CCSprite to stop them individually before running the next action on it.
To help create some context, lets assume the following situation:
We have a CCSprite called mySprite
We have 3 separate CCAnimation actions defined for walking to the right, walking to the left, and sitting looking forward called: actionAnimWalkRight, actionAnimWalkLeft, and actionAnimSitForward respectively.
We want to have the sprite walk to the right when someone touches the screen right of mySprite, walk left when someone touches the screen left of mySprite and sit when someone touches mySprite.
The approach I'm using to accomplish this is as follows:
Place CCSprite as a child in the scene.
Tell the sprite to run an action using: [self runAction:actionWalkRight];
When I want to change the action after someone touches, I have a method called stopAllAnimationActions which I call before I apply a new action that stops any animation action no matter what's running. Basically lists ALL the CCAnimation/CCActions I have defined and stops each one individually since I don't want to use stopAllActions. as follows: [self stopAction:actionWalkRight]; [self stopAction:actionWalkLeft]; [self stopAction:actionSitForward];
Then I apply the new animation after the above method fires using: [self runAction:actionWalkLeft];
While this works, it just seems like a poor design to stop items that I know aren't running just because I don't know exactly what is running. So just looking for advice and the best recommended practice to do something like this within very complex situations so tracking every possible situation is difficult. Any feedback would be appreciated.
When creating the actions set the tag of that action with a constant:
actionWalkRight.tag= kCurrentAction;
[self runAction:actionWalkRight];
Then, retrieve the running action by that tag and stop it.
[self stopActionByTag:kCurrentAction];
I recommend you simplify your process and take advantage of the native Cocos features, including stopAllActions. Don't re-use actions, always create them from scratch as it has been well discussed among Cocos developers that re-using actions can be buggy.
Cocos is well optimized and features like stopAllActions are not performance hogs. It would probably be faster than your approach, actually.