Undisplayed view crash in cocos2D - ios

There are no errors or warning but this crashes when I try and switch to the view:
#import "Level1.h"
#import "LevelSelect.h"
#import "GameOver.h"
enum {
kTagPlayer, kTagComputer
};
#implementation Level1
+(id) scene {
CCScene *scene = [CCScene node];
Level1 *layer = [Level1 node];
[scene addChild: layer];
return scene;
}
-(id) init {
if( (self=[super init] )) {
CCSprite *player = [CCSprite spriteWithFile:#"Icon_Small_50.png"];
player.position = ccp(60, 60);
[self addChild:player z:2 tag:kTagPlayer];
CCSprite *computer = [CCSprite spriteWithFile:#"Icon_Small.png"];
computer.position = ccp(440, 160);
[self addChild:computer z:1 tag:kTagComputer];
[computer runAction:[CCMoveTo actionWithDuration:3.0
position:ccp(player.position.x, player.position.y)]];
self.isTouchEnabled = YES;
[self schedule:#selector(SpritesDidColide)
interval:.01];
}
return self;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point];
CCNode *player = [self getChildByTag:kTagPlayer];
[player setPosition:point];
CCNode *computer = [self getChildByTag:kTagComputer];
[computer runAction:[CCMoveTo actionWithDuration:3.0
position:ccp(player.position.x, player.position.y)]];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point];
CCNode *player = [self getChildByTag:kTagPlayer];
[player setPosition:point];
CCNode *computer = [self getChildByTag:kTagComputer];
[computer runAction:[CCMoveTo actionWithDuration:3.0
position:ccp(player.position.x, player.position.y)]];
}
-(void) SpritesDidCollide {
CCNode *player = [self getChildByTag:kTagPlayer];
CCNode *computer = [self getChildByTag:kTagComputer];
float xDif = computer.position.x - player.position.x;
float yDif = computer.position.y - player.position.y;
float distance = sqrt(xDif * xDif + yDif * yDif);
if (distance < 45) {
[self unschedule:#selector(SpritesDidCollide)];
[[CCDirector sharedDirector] replaceScene:[CCTransitionScene transitionWithDuration:1 scene:[GameOver node]]];
}
}
#end
And here is what displays in the Console:
* Assertion failure in -[CCTimer initWithTarget:selector:interval:], /Users/GLaDOS/Documents/Xcode 4 Projects/2DPlatformer/2DPlatformer/libs/cocos2d/CCScheduler.m:110
2012-02-17 19:13:07.460 2DPlatformer[709:707] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Signature not found for selector - does it have the following form? -(void) name: (ccTime) dt'
** First throw call stack:
(0x37f1d8bf 0x31a031e5 0x37f1d7b9 0x31e1c3b3 0x46565 0x4843f 0x3402d 0x948d3 0x32249 0x93a23 0x37f20814 0x37e7b7e1 0x2de31 0x2bdc9 0x37e77435 0x7087f 0x71019 0x727ff 0x352ef50f 0x352eef01 0x352d54ed 0x352d4d2d 0x379f2df3 0x37ef1553 0x37ef14f5 0x37ef0343 0x37e734dd 0x37e733a5 0x379f1fcd 0x35303743 0x912c7 0x2300)
terminate called throwing an exceptionkill
Current language: auto; currently objective-c
quit
Program ended with exit code: 0

I believe the schedule method will trigger a call back to a selector of the form:
-(void) SpritesDidCollide:(ccTime) dt{
// do your thing here.
}
and change the selector to
[self schedule:#selector(SpritesDidCollide:) interval:.01];

Related

Sprites and Bounding Box issues

I am making an IOs game for an assignment using cocos2D. So far I am just trying to get all of the menus done, but I am having some issues with bounding box when setting up some of my buttons.
The game starts with the cocos2D logoscreen, then transits into a splash screen for my game, then into the main menu. In the game menu I have 2 buttons: Begin, and Options. Both buttons are working perfectly fine, each makinga transicion to the correct scene.
I am currently working on the options menu scene, where I have a button to take care of volume, a butto to set game difficulty, and a button to go back to main menu. I have been trying to make the back button work, but is not triggering when touched, I placed an NSLog for debugging, and the touches are being detected, just the if statement that checks whether the sprite was touched oris never true. I am puzzled by this, since I am using the same method I used in my main menu (which works). I searched for similar issues before posting, but everything I tried, like altering the parent coordinate system and/or the sprites boundingbox.origin, haven't worked at all.
This is the code for my Options Scene:
#import "OptionsMenu.h"
#implementation OptionsMenu
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
OptionsMenu *layer = [OptionsMenu node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
self.touchEnabled = YES;
self.accelerometerEnabled = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"Menu0.png"];
background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:background z:0];
CCSprite *volumeBar = [CCSprite spriteWithFile:#"VolumeBar.png"];
volumeBar.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:volumeBar z:1];
CCSprite *volumeButton = [CCSprite spriteWithFile:#"VolumeButton.png"];
volumeButton.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:volumeButton z:1];
CCSprite *difficultyButton = [CCSprite spriteWithFile:#"EasyButton.png"];
difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
[self addChild:difficultyButton z:1];
CCSprite *backButton = [CCSprite spriteWithFile:#"BackButton.png"];
backButton.position = ccp(winSize.width/12, winSize.height/8);
[self addChild:backButton z:1];
[self scheduleUpdate];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
CCActionInterval *actionInterval = [CCActionInterval actionWithDuration:0.1];
if (CGRectContainsPoint([self.backButton boundingBox], location)) {
[self.backButton setTexture:[[CCTextureCache sharedTextureCache] addImage:#"BackButton.png"]];
id actionCallFunc = [CCCallFunc actionWithTarget:self selector:#selector(goToMainMenu:)];
[self.background runAction: [CCSequence actions: actionInterval, actionCallFunc, nil]];
}
else if (CGRectContainsPoint([self.difficultyButton boundingBox], location)) {
[self.volumeButton setTexture:[[CCTextureCache sharedTextureCache] addImage:#"VolumeButton.png"]];
}
}
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
NSLog(#"Touch");
//missileButton* missileButton;
location = [[CCDirector sharedDirector] convertToGL: location];
if (CGRectContainsPoint([self.backButton boundingBox], location))
{
[self.backButton setTexture:[[CCTextureCache sharedTextureCache]addImage:#"BackButtonPressed.png"]];
NSLog(#"BackButtonTouched");
}
else if (CGRectContainsPoint([self.volumeButton boundingBox], location))
[self.volumeButton setTexture:[[CCTextureCache sharedTextureCache]addImage:#"VolumeButtonPressed.png"]];
}
}
-(void) goToMainMenu:(id) sender
{
[[CCDirector sharedDirector] replaceScene: [CCTransitionSlideInR transitionWithDuration:2 scene:[MainMenu node]]];
}
#end
I would appreciate any help in solving this issue.
Your self.backButton is never defined. Change your init function to the following (I'm assuming all of your CCSprite defines are actually properties:
-(id) init
{
if( (self=[super init]) ) {
self.touchEnabled = YES;
self.accelerometerEnabled = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
_background = [CCSprite spriteWithFile:#"Menu0.png"];
_background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_background z:0];
_volumeBar = [CCSprite spriteWithFile:#"VolumeBar.png"];
_volumeBar.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_volumeBar z:1];
_volumeButton = [CCSprite spriteWithFile:#"VolumeButton.png"];
_volumeButton.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_volumeButton z:1];
_difficultyButton = [CCSprite spriteWithFile:#"EasyButton.png"];
_difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
[self addChild:_difficultyButton z:1];
_backButton = [CCSprite spriteWithFile:#"BackButton.png"];
_backButton.position = ccp(winSize.width/12, winSize.height/8);
[self addChild:_backButton z:1];
[self scheduleUpdate];
}
return self;
}
Another approach would be to use CCMenu and CCMenuItemSprite for all of your buttons. These classes make simple buttons for menus much easier to implement. Most/all of the touch handling is done for you and there are variants that use selectors or blocks for handling the button press.
This would save you a lot of code, and trouble.

Moving sprites in cocos2d

I have 6 sprites that I want to move around:
-(id) init
{
if( (self=[super init]) )
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite * backGround = [CCSprite spriteWithFile:#"background_ipad.png"];
backGround.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:backGround z:0];
cloth1 = [CCSprite spriteWithFile:#"clothe_1.png"];
cloth1.position = ccp(-200, -15);
[self addChild:cloth1 z:1];
cloth2 = [CCSprite spriteWithFile:#"clothe_2.png"];
cloth2.position = ccp(130, 225);
[self addChild:cloth2 z:2];
cloth3 = [CCSprite spriteWithFile:#"clothe_3.png"];
cloth3.position = ccp(365, 225);
[self addChild:cloth3 z:3];
cloth4 = [CCSprite spriteWithFile:#"clothe_4.png"];
cloth4.position = ccp(-110, -15);
[self addChild:cloth4 z:4];
cloth5 = [CCSprite spriteWithFile:#"clothe_5.png"];
cloth5.position = ccp(130, -20);
[self addChild:cloth5 z:5];
cloth6 = [CCSprite spriteWithFile:#"clothe_6.png"];
cloth6.position = ccp(365, -15);
[self addChild:cloth6 z:6];
}
return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
and this method to move:
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
//Sprite follows the finger movement.
[cloth1 setPosition:location];
}
and the thing is that, I want to add more sprites there to move the sprite. I triend adding more sprites in the //follow the finger movement but then all sprites follow the finger movement. I want to move one single sprite. For example: when touching cloth1, move cloth 1; when touching cloth2, move cloth 2; but not both at the same time.
Can someone tell me how to do this?
#interface YourClass : NSObject
{
NSMutableArray *mSpriteArray;
CCSprite *mSpriteOnHand;
}
//In implementation:
-(id) init
{
.. //your old code
..
[mSpriteArray addObject: cloth1];
[mSpriteArray addObject: cloth2];
[mSpriteArray addObject: cloth3];
[mSpriteArray addObject: cloth4];
[mSpriteArray addObject: cloth5];
[mSpriteArray addObject: cloth6];
}
-(void)onEnter
{
[super onEnter];
self.touchEnabled = YES; // self.isTouchEnabled = YES;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
mSpriteOnHand = nil;
for(CCSprite *cloth in mSpriteArray)
{
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if(mSpriteOnHand)
mSpriteOnHand.position = location;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
mSpriteOnHand = nil;
}
-(void)onExit
{
[mSpriteArray release];
mSpriteArray = nil;
[super onExit];
}
return statement always terminates function and returns control to the calling function. You wrote:
return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher]... line will never get executed.
Is your class a subclass of CCLayer? If you set layer's isTouchEnabled property to YES, it will add this layer as standard (non-targeted) touch delegate.
If you must use targeted touch in your layer, you should return YES in -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event if you are claiming this touch, and NO if you aren't. Updates of a claimed touch are sent only to the delegate which claimed it.
To determine which sprite is touched: make an instance variable to store "selected" sprite, in touchBegan method check which sprite's bounding box contains touch location and store this sprite in instance variable (also, looks like you want to claim a touch only if it's touching a sprite).
[cloth1 setPosition:location];
-- your sprite will "jump" to touch position. Usually it doesn't look nice. I would get touch's locationInView: and previousLocationInView:, convert them to GL, get the difference and change sprite's position by that difference.

Linking HUDLayer to GameplayLayer properly

I'm trying to pass the touch location from GameplayLayer to HUDLayer to see if user presses control buttons.
HudLayer.mm
-(void) handleTouchAtLocation:(CGPoint) location {
NSLog(#"Touch passed to HUD");
}
Gameplay.mm
enum {
kHudLayer = 2;
};
+(CCScene *) scene {
CCScene *scene = [CCScene node];
HudLayer *hud = [HudLayer node];
[scene addChild:hud z:kHudLayer];
GameplayLayer *layer = [GameplayLayer node];
[scene addChild:layer];
return scene;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
[self handTouchAtPoint:(location)];
}
}
-(void) handleTouchAtPoint:(CGPoint)location {
NSLog(#"Touch At Point");
HudLayer *h1 = (HudLayer *)[self.parent getChildWithTag:kHudLayer];
[h1 handleTouchAtLocation:location];
}
HudLayer.h is imported in GameplayLayer.h.
I'm getting the "Touch At Point" log but it's not going through to HUD layer for some reason..
The only explanation is that self.parent has no child with the tag kHudLayer. If you set a breakpoint in handleTouchAtPoint you'll notice h1 being nil after the getChildWithTag line did execute.

CGRect and touch

In this cocos2d app the nslog is not firing when I press the ccsprite. Could someone help me?
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = CGRectMake(target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
27,
40);
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if (CGRectContainsPoint(targetRect, touchLocation)) {
NSLog(#"Moo cheese!");
}
}
return YES;
}
First of all be sure that you register the sprite for touches into the onEnter method for example:
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:defaultTouchPriority_ swallowsTouches:YES];
[super onEnter];
}
This will make your sprite touchable and so fire the event to the sprite when a user will press it.
Then refactor your code to make it more readable and test something like that:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
NSArray *targetsToDelete = [self touchedSpritesAtLocation:touchLocation];
// Put your code here
// ...
return YES;
}
- (NSArray *)touchedSpritesAtLocation:(CGPoint)location
{
NSMutableArray *touchedSprites = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets)
if (CGRectContainsPoint(target.boundingBox, location))
[touchedSprites addObject:target];
return [touchedSprites autorelease];
}
It should return the targets that have been touched.
In layer init method add this
self.isTouchEnabled = true;
Use this code for touch detection
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CGRect rect = [self getSpriteRect:yourSprite];
if (CGRectContainsPoint(rect, location))
{
NSLog(#"Sprite touched\n");
}
}
To get sprite rect:
-(CGRect)getSpriteRect:(CCNode *)inSprite
{
CGRect sprRect = CGRectMake(
inSprite.position.x - inSprite.contentSize.width*inSprite.anchorPoint.x,
inSprite.position.y - inSprite.contentSize.height*inSprite.anchorPoint.y,
inSprite.contentSize.width,
inSprite.contentSize.height
);
return sprRect;
}

Cocos 2d draw line doesn't work

I want to draw line on finger touch in cocos 2d.
-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
UITouch *touch = [inappropriateTouches anyObject];
CGPoint currentTouchArea = [touch locationInView:[touch view] ];
CGPoint lastTouchArea = [touch previousLocationInView:[touch view]];
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
// throw to console my inappropriate touches
NSLog(#"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(#"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
glColor4f(0.8, 1.0, 0.76, 1.0);
glLineWidth(6.0f);
ccDrawLine(currentTouchArea, lastTouchArea);
}
I use this code but nothing is drawn on the screen. What is wrong in my code?
All OpenGL drawing you want to do in the draw method. Like this:
-(void)draw
{
if(lastTouchArea != 0)
{
glColor4f(0.8, 1.0, 0.76, 1.0);
glLineWidth(6.0f);
ccDrawLine(currentTouchArea, lastTouchArea);
lastTouchArea = 0;
}
}
Try this : to save a line in a NSMutableArray
-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
UITouch *touchMyMinge = [inappropriateTouches anyObject];
CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];
// flip belly up. no one likes being entered from behind.
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
// throw to console my inappropriate touches
NSLog(#"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(#"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
// add my touches to the naughty touch array
naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}
#implementation DrawMyTouch
-(id) init
{
if( (self=[super init]))
{ }
return self;
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for(int i = 0; i < [naughtyTouchArray count]; i+=2)
{
start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
Hope this help

Resources