Currently I'm fixing an old Cocos2D iPhone 1.0 game. There is a strange problem: when the application is being sent to background by the four-finger gesture(iPad) after returning it behaves like the touch began and didn't end, and there no way to end it because the application does not respond to touches. Also when I launch the application and do nothing, the screen becomes locked and after that the application some times does not respond to touches.
I guess that there is something I need to do in the following AppDelegate methods, but I can't figure out exactly:
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
enter code here
- (void)applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
}
The problem was in -(void)ccTouchCanceled method not implemented
Related
In my HelloWorldLayer code I am pushing a SettingsLayer scene onto the stack.
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5 scene:[SettingsLayer scene]]];
The SettingsLayer needs to call a delegate method implemented in the HelloWorldLayer. But I am at a lost as to how I can set the HelloWorldLayer as the SettingsLayer's delegate. Can someone show me the proper pattern for this? I tried alloc-init of the SettingsLayer, and setting the delegate before the pushscene but this did not work.
In your code:
SettingsLayer *mySettingsLayer = [[SettingsLayer alloc] init];
mySettingsLayer.delegate = self;
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5
scene:[[mySettingsLayer class] scene]]];
The problem is in the last line:
[[mySettingsLayer class] scene]
This creates a new instance of the SettingsLayer class which has no delegate set (it will be nil). The mySettingsLayer you created before isn't actually presented as a scene, it goes out of scope and deallocates (assuming you are using ARC, if not, this would actually leak the mySettingsLayer object).
To fix it pass in your already existing mySettingsLayer instance:
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5
scene:mySettingsLayer]];
I'm porting a code to ios7 in wich I used old GKPeerPickerController and cocos2d to make a multiplayer game over Bluetooth.
In the new version, I'm using the MCBrowserViewController, but im unable to show it on the scene.
Im calling it with
[[[CCDirector sharedDirector] view] addSubview: browserVC];
where browserVC is an initiallized MCBrowserViewController but as this is only to show UIViews I get the error
Cannot initialize a parameter of type 'UIView *' with an rvalue of
type 'MCBrowserViewController *'
Is there any way to show the MCBrowserViewController on the scene?
Thanks in advance!
Should work
[[CCDirector sharedDirector] presentViewController:browserVC animated:YES completion:nil];
So in my project I am using Cocos2D with CocosBuilder. I have assigned a few of my characters to be subclasses of CCNode with child CCSprites, etc.
I want these CCNodes to be reactive to touch - for example, if I touch any of them, they'd play a context sensitive animation. I only want to know how to make the node reactive to touch (or perhaps, having the layer reactive to touch which detects whether you've touched a sprite or not), the animation part is fine.
Any ideas? that would be lovely.
Sam
Turns out that this is fairly easy. In the header file of your class, you must define the class as implementing the protocol , like so:
#interface Foo : CCNode <CCTouchOneByOneDelegate>
{
}
and you must implement onEnter and onExit like this:
- (void)onEnter
{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit
{
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
[super onExit];
}
and you must implement ccTouchBegan (if you're using the OneByOneDispatcher)
Evening all,
I'll start this question in the time honoured tradition by saying that I've had a good old search on SO and also in the wider world but I'm not quite getting my head around this...
I've implemented a sneakyJoystick which works wonderfully (It moves my sprite around quite happily) however I've now done myself a mischief in thinking about it's positioning.
What I'd like to do is simply change it's position to a touch location and have it move my sprite around but this seems to be out of my knowledge pool. I might be being an idiot but I cannot work it out.
The touch events are already sorted in the sneakyjoystick classes (available on github https://github.com/0xPr0xy/sneaky-joystick-cocos2d). At the moment if I create the joystick during the init method in a class called controlsLayer then everything works fine; Joystick appears and it allows me to move to sprite around
-(id) init
{
if( (self=[super init]) ) {
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = ccp(64, 64);
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
[self scheduleUpdate];
}
return self;
}
So to begin with I thought about looking at how I could simply get it to show and hide itself and set it's location. To do this I created a ccTouchesbegan method which contains pretty much the same code as my init method did before. This works fine up to a point (the joystick appears centred wherever I touch) but the issue now is that I cannot interact with it. The joystick appears where i want but it will not recognise my movements (the stick on the joystick does not move which in turn means that my sprite is not being told to move either)
if( (self=[super init]) ) {
self.isTouchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = location;
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeChild:myJoystickBase cleanup:YES];
}
So to my uninitiated brain this is saying that when I touch the controlsLayer class it happily does what I've asked it to but it will not then pass this touch to other classes.
In a nutshell can a touch event be passed to multiple classes at the same time? The sneaky joystick class uses CCTargetedTouchDelegate which worked fine when it was the only thing looking for a touch. However now that I've added a -(void)ccTouchesBegan: in another class it's not happy.
Can anyone tell me if the problem is with the way I'm handling touches or is it possibly an issue with the way that I allocate the joystick in the touch method? Should I be allocating the joystick in the init method and be doing something else in the touchesBegan method? Trial and error isn't getting me anywhere useful at the moment. Feel like I'm banging my head against a brick wall. I'm happy to post up the full class files if necessary. Does this make sense?
The problem is that you create the joystick during touch began, and remove it on touch ended. This does not allow the joystick to receive a touch began event, since the event is already being processed at the time the joystick is being created.
You're also leaking the joystick due to the unnecessary retain (use ARC, please!).
Try creating the joystick in init, disable & hide it until a touch began event is received. This is also faster than recreating the joystick on every touch.
Also, should you have multiple touches enabled, keep in mind that you can receive up to 5 touch began events (5 fingers) without any touch ended in between. That would create 5 joysticks, but only remove one! Every time!
I have developed a game. When i checked in iphone, i found that my game pause when an incoming call comes and restarts after attending and ending the call. How to make it resume instead of restart
Try checking the AppDelegate.m for this:
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
That usually "pauses" the current scene when the application is interupted...say for a phone call.
If this doesn't help...maybe you could eyplain your problem further.
greetings