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.
Related
I have a CCLayer class imported onto a game layer
strangely all the the sprites have "artefacts" that seem to appear from nowhere since I have checked and re-exported all of the files
Is some setting or something else that could cause this to happen?
I'm new at this
but I have checked so far:
set to PixelFormat_RGBA8888
PVRImagesHavePremultipliedAlpha:YES
png's are clear from artefact (28bit with transparency)
Textures are made with texture packer with "pre-multiplied"
The Background is a CCLayer
The Mine is a CCLayer
both are added to the game layer (cclayer also) as "addChild"
backgroundManager = [[BackGround alloc] init];
[self addChild:backgroundManager z:0];
myShip = [[Ship alloc]init];
[self addChild:myShip z:5];
Yes it was the settings in texture packer
after a few changes in the settings they now seem to load fine.
with no artifacts
use pre multiple
trim not crop
and give a little more inner padding
hope it helps someone else (since it was driving me a little crazy)
CCActionCallBlock *call=[CCActionCallBlock actionWithBlock:^
{
NSLog(#"***********done");
}];
[self runAction:call];
Is not working if i put it in another node, and add this node to my main scene .
It only works in my main scene, and not in any other layer added to that scene .
why ?
I just tried it and it works for me, have you called [super onEnter] on your scene?
If that does not work please a bit more code please.
I am trying to take a particle effect in my Cocos2d project. Particle effect shows good. But I am confused when I put the particle showing function into a thread as follows, it only shows tiny dark squares instead of the right texture. thanx in advance.
// thread caller
[self performSelectorInBackground:#selector(showParticleThrd:) withObject:nil];
// it works good when i call it directly
-(void) showParticleThrd{
CCParticleSystem * system = [[ParticleEffectSelfMade alloc] initWithTotalParticles:1];
[aLayer removeChildByTag:R_TAG cleanup:YES];
system.position = ccp(self.position.x,self.position.y);
[self.parent addChild:system z:R_ORDER tag:R_TAG];
[system release];
}
You can not modify anything cocos2d related in a background thread. Cocos2d requires you to make changes to nodes in the main thread, where the OpenGL context was created.
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.
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;.