i created a project by using cocos2d,and now i want to use UISwipeGestureRecognizer for get the up/down/left/right, so how can i do?
thanks a lot
In the .h file add this :
// Add inside #interface
UISwipeGestureRecognizer * _swipeLeftRecognizer;
UISwipeGestureRecognizer * _swipeRightRecognizer;
// Add after #interface
#property (retain) UISwipeGestureRecognizer * swipeLeftRecognizer;
#property (retain) UISwipeGestureRecognizer * swipeRightRecognizer;
In .m file add this :
// Add after #implementation
#synthesize swipeLeftRecognizer = _swipeLeftRecognizer;
#synthesize swipeRightRecognizer = _swipeRightRecognizer;
// Then add these new methods
- (void)onEnter {
self.swipeLeftRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe:)] autorelease];
_swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeLeftRecognizer];
self.swipeRightRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe:)] autorelease];
_swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeRightRecognizer];
}
- (void)onExit {
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeLeftRecognizer];
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeRightRecognizer];
}
// Add to dealloc
_swipeLeftRecognizer = nil;
[_swipeRightRecognizer release];
_swipeRightRecognizer = nil;
Hope it'll help
Related
In my effort to save a ScrollingBackground object I've subclassed the CCSprites to conform to NSCoding. The ScrollingBackground doesn't display. Please see the relevant code below. I'm not really sure whats wrong. Please help.
ScrollingBackground.h:
(CCBackgroundSprite's interface)
#interface CCBackgroundSprite: NSObject <NSCoding>
#property (nonatomic, assign) float xValue;
#property (nonatomic, assign) float yValue;
#property (nonatomic, retain) NSString* backgroundStringName;
#end
ScrollingBackground.m:
(CCBackgroundSprite's implementation)
#implementation CCBackgroundSprite
-(id)init
{
if((self = [super init])){
}
return self;
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
self.xValue = [aDecoder decodeFloatForKey:#"xValue"];
self.yValue = [aDecoder decodeFloatForKey:#"yValue"];
self.backgroundStringName = [aDecoder decodeObjectForKey:#"backgroundStringName"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeFloat:self.xValue forKey:#"xValue"];
[aCoder encodeFloat:self.yValue forKey:#"yValue"];
[aCoder encodeObject:self.backgroundStringName forKey:#"backgroundStringName"];
}
#end
Setting CCBackgroundSprite's instances for the CCSprite properties:
-(void)spriteProperties {
background1 = [[CCBackgroundSprite alloc] init];
[background1 setXValue:bg.position.x];
[background1 setYValue:bg.position.y];
[background1 setBackgroundStringName:#"bg"];
background2 = [[CCBackgroundSprite alloc] init];
[background2 setXValue:bgSwap.position.x];
[background2 setYValue:bgSwap.position.y];
[background2 setBackgroundStringName:#"bgSwap"];
background3 = [[CCBackgroundSprite alloc] init];
[background3 setXValue:bgSwap2.position.x];
[background3 setYValue:bgSwap2.position.y];
[background3 setBackgroundStringName:#"bgSwap2"];
}
encoding/decoding of other non-Sprite related properties of the ScrollingBackground:
-(void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:self.backgroundCount forKey:#"backgroundCount"];
[aCoder encodeInt:self.backgroundRepeatCount forKey:#"backgroundRepeatCount"];
[aCoder encodeFloat:self.scrollSpeed forKey:#"scrollSpeed"];
[aCoder encodeObject:self.backgroundArray forKey:#"backgroundArray"];
[aCoder encodeObject:self.changeArray forKey:#"changeArray"];
.
.
.
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
self.backgroundCount = [aDecoder decodeIntForKey:#"backgroundCount"];
self.backgroundRepeatCount = [aDecoder decodeIntForKey:#"backgroundRepeatCount"];
self.scrollSpeed = [aDecoder decodeFloatForKey:#"scrollSpeed"];
self.backgroundArray = [aDecoder decodeObjectForKey:#"backgroundArray"];
self.changeArray = [aDecoder decodeObjectForKey:#"changeArray"];
.
.
.
}
}
Saving and loading of ScrollingBackground object:
- (void)saveBackgroundObject:(ScrollingBackground *)object key:(NSString *)key {
[self spriteProperties];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
NSString *dataToString = [NSString stringWithFormat:#"%#", encodedObject];
CCLOG(#"encodedObject = %# \n", dataToString);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:key];
[defaults synchronize];
}
-(ScrollingBackground *)loadBackgroundWithKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [defaults objectForKey:key];
NSString *dataToString = [NSString stringWithFormat:#"%#", encodedObject];
CCLOG(#"encodedObject = %# \n", dataToString);
ScrollingBackground *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
return object;
}
UPDATED:
I have made the following changes the spriteProperties method:
-(void)spriteProperties {
background1 = [[CCBackgroundSprite alloc] init];
[background1 setXValue:bg.position.x];
[background1 setYValue:bg.position.y];
[background1 setBackgroundImageName:bg.displayFrame.textureFilename];
[self addChild:background1];
background2 = [[CCBackgroundSprite alloc] init];
[background2 setXValue:bgSwap.position.x];
[background2 setYValue:bgSwap.position.y];
[background2 setBackgroundImageName:bgSwap.displayFrame.textureFilename];
[self addChild:background2];
background3 = [[CCBackgroundSprite alloc] init];
[background3 setXValue:bgSwap2.position.x];
[background3 setYValue:bgSwap2.position.y];
[background3 setBackgroundImageName:bgSwap2.displayFrame.textureFilename];
[self addChild:background3];
}
The main reason I am using displayFrame.textureFilename above is because I'm reusing the sprites along the way.
Also to setup of the background images I did:
-(void)startingSprites //change later to setupInitialBackground
{
CGSize s = [[CCDirector sharedDirector] winSize];
bg = [CCSprite spriteWithSpriteFrameName:#"bgImage1.png"];
bg.position = ccp(s.width/2, s.height/2);
[currentBackgroundBatchNode addChild:bg];
swapbg = [CCSprite spriteWithSpriteFrameName:#"bgImage2.png"];
swapbg.position = ccp(s.width/2, 3*s.height/2 -1.0);
[currentBackgroundBatchNode addChild: swapbg];
swapbg2 = [CCSprite spriteWithSpriteFrameName:#"bgImage3.png"];
swapbg2.position = ccp(s.width/2, 5*s.height/2 - 2.0);
[currentBackgroundBatchNode addChild: swapbg2];
CCLOG(#"bg background is %#", bg.displayFrame.textureFilename);
CCLOG(#"bgSwap background is %#", swapbg.displayFrame.textureFilename);
CCLOG(#"bgSwap2 background is %#", swapbg2.displayFrame.textureFilename);
}
I've just realized a few things:
the CCLOG's in startingSprites are null
I reuse the currentBackgroundBatchNode (which is a CCSpriteBatchNode) along the way, meaning that I have to encode/decode it. How do I subclass it and with what properties? Not too sure how it'll work out.
I have read a number of your posts, also related to this. I would recommend that instead of trying to subclass several cocos2d classes to conform to NSCoding you should use a simpler work around. I believe your background has it's own layer, so why don't you rather save various background parameters and create another init for your background to handle cases for reloading the background state.
You say you've subclassed CCSprite, but you actually subclassed NSObject. Try:
#interface CCBackgroundSprite: CCSprite <NSCoding>
...
I have subclassed a CCSprite to make an object that can be encoded and decoded. I want to save sprites state and load it again at particular positions. Everything seems to be okay apart from decoding with NSKeyedUnarchiver (see loadIconSpriteState below) which gives an EXC_BAD_ACCESS Below is the code:
HelloWorldLayer.h
#interface CCIconSprite : CCSprite {
NSString *iconName;
float iconXPos;
float iconYPos;
}
#property (nonatomic, retain) NSString *iconName;
#property (nonatomic, assign) float iconXPos;
#property (nonatomic, assign) float iconYPos;
+ (id)iconWithType:(NSString*)imageName;
- (id)initWithIconType:(NSString*)imageName;
#end
#interface HelloWorldLayer : CCLayer < NSCoding>
{
CCIconSprite* testSprite;
BOOL savedState;
CGSize size;
CCMoveTo* moveTo;
NSMutableArray* saveSpriteArray;
NSData* savedSpriteData;
}
+(CCScene *) scene;
#end
HelloWorldLayer.m:
CCIconSprite implementation:
#implementation CCIconSprite
#synthesize iconXPos;
#synthesize iconYPos;
#synthesize iconName;
+ (id)iconWithType:(NSString*)imageName
{
return [[[[self class] alloc] initWithIconType:imageName] autorelease];
}
- (id)initWithIconType:(NSString*)imageName
{
self = [super initWithFile:imageName];
if (!self) return nil;
iconName = imageName;
self.position = ccp(iconXPos, iconYPos);
return self;
}
Encoding and decoding:
- (id)initWithCoder:(NSCoder *)decoder {
NSString* imageFileName = [decoder decodeObjectForKey:#"imageFileName"];
self = [self initWithIconType:imageFileName];
if (!self) return nil;
self.iconXPos = [decoder decodeFloatForKey:#"iconXPos"];
self.iconYPos = [decoder decodeFloatForKey:#"iconYPos"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:iconName forKey:#"imageFileName"];
[encoder encodeFloat:self.iconXPos forKey:#"iconXPos"];
[encoder encodeFloat:self.iconYPos forKey:#"iconYPos"];
}
#end
HelloWorldLayer implementation:
#implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
self.scale = 0.5;
savedState = NO;
size = [[CCDirector sharedDirector] winSize];
testSprite = [CCIconSprite spriteWithFile:#"Icon.png"];
testSprite.position = ccp(size.width/4, size.width/4);
testSprite.anchorPoint = ccp(0,0);
[self addChild:testSprite];
moveTo = [CCMoveTo actionWithDuration:3 position:ccp(3*size.width/4, 3*size.width/4)];
[testSprite runAction:moveTo];
[self schedule:#selector(saveAndLoadSpriteState)];
}
return self;
}
Saving and loading state:
-(void)saveIconSpriteState {
saveSpriteArray = [[NSMutableArray alloc] init];
[saveSpriteArray addObject:testSprite];
savedSpriteData = [NSKeyedArchiver archivedDataWithRootObject:saveSpriteArray];
}
-(void)loadIconSpriteState {
[NSKeyedUnarchiver unarchiveObjectWithData:savedSpriteData];
}
-(void)saveAndLoadSpriteState {
if ((testSprite.position.x > size.width/2) && !savedState) {
savedState = YES;
[self saveIconSpriteState];
}
else if ((testSprite.position.x == 3*size.width/4) && savedState) {
savedState = NO;
[self loadIconSpriteState];
[testSprite runAction:moveTo];
}
}
#end
EDIT
After setting an exception break point I got the following error
Assertion failure in -[CCIconSprite initWithFile:]
pointing to the in line:
NSAssert(filename != nil, #"Invalid filename for sprite");
in the
-(id) initWithFile:(NSString*)filename
method of the CCSprite.m class.
Just a hunch but maybe that's it. When decoding a string, you should copy it because you don't own it at this point, nor are you assigning it to a strong or copy property.
- (id)initWithCoder:(NSCoder *)decoder {
NSString* imageFileName = [[decoder decodeObjectForKey:#"imageFileName"] copy];
self = [self initWithIconType:imageFileName];
if (!self) return nil;
...
}
If that's not it, set a breakpoint and verify that the string is indeed correct when encoding and when decoding.
firstly i would like to apologise for being a complete novice but I have researched the web and can not find a solution to my current problem.
I have 3 objects (sprites) that i have set up individually with switch statements and want to use my main gameplay layer to change their states randomly to an animation that i have already defined in the objects individual header and implementation files.
I have set up a self schedule updater and arc4random method that works but it will not change the state of the object, as it only calls the CCLOG that I have also included in the statement.
I have listed to code below and i know it is a bit of a mess but still very much in my first steps of being a beginner, if anyone can point me in the right direction (that's if i have explained this in a way you can understand!) I would be very grateful.
Thanks in advance for even looking at this question.
//-------------------below is my gameplaylayer header file---------//
// GamePlayLayer.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCLayer.h"
#import "SneakyJoystick.h"
#import "SneakyButton.h"
#import "SneakyButtonSkinnedBase.h"
#import "SneakyJoystickSkinnedBase.h"
#import "Constants.h"
#import "CommonProtocols.h"
#import "TBT.h"
#import "MBT.h"
#import "BBT.h"
#import "BC.h"
#import "GameCharacter.h"
#import <stdlib.h>
#interface GamePlayLayer : CCLayer <GamePlayLayerDelegate> {
CCSprite *vikingSprite;
SneakyJoystick *leftJoystick;
SneakyButton *jumpButton;
SneakyButton *attackButton;
CCSpriteBatchNode *sceneSpriteBatchNode;
}
#property (readwrite) CharacterStates characterState;
-(void)changeState:(CharacterStates)newState;
-(void)addEnemy;
#end
//---------------------Below is my gameplaylayer implementation file-------------//
// GamePlayLayer.m
#import "GamePlayLayer.h"
#implementation GamePlayLayer
#synthesize characterState;
-(void) dealloc {
[leftJoystick release];
[jumpButton release];
[attackButton release];
[super dealloc];
}
-(void)initJoystickAndButtons {
CGSize screenSize = [CCDirector sharedDirector].winSize;
//---DELETED MOST OF THE ABOVE METHOD AS NOT NEEDED FOR THIS QUESTION----//
-(void) update:(ccTime)deltaTime {
CCArray *listOfGameObjects =
[sceneSpriteBatchNode children];
for (GameCharacter *tempChar in listOfGameObjects) {
[tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];
}
}
-(void) createObjectOfType: (GameObjectType)objectType
withHealth:(int)initialHealth atLocation:(CGPoint)spawnLocation withZValue:(int)ZValue {
if (objectType == kEnemyType1BT) {
CCLOG(#"creating the 1BT");
TBT *tBT = [[TBT alloc] initWithSpriteFrameName:#"BT_anim_1.png"];
[tBT setCharacterHealth:initialHealth];
[tBT setPosition:spawnLocation];
[sceneSpriteBatchNode addChild:tBT
z:ZValue
tag:k1BTtagValue];
[tBT release];}
if (objectType == kEnemyType3BT){
CCLOG(#"creating the radar enemy");
BBT *bBT = [[BBT alloc] initWithSpriteFrameName:#"BT_anim_1.png"];
[bBT setCharacterHealth:initialHealth];
[bBT setPosition:spawnLocation];
[sceneSpriteBatchNode addChild:bBT
z:ZValue
tag:k3BTtagValue];
[bBT release];
}
if (objectType == kEnemyType2BT){
CCLOG(#"creating the radar enemy");
MBT *mBT = [[MBT alloc] initWithSpriteFrameName:#"BT_anim_1.png"];
[mBT setCharacterHealth:initialHealth];
[mBT setPosition:spawnLocation];
[sceneSpriteBatchNode addChild:mBT
z:ZValue
tag:k2BTtagValue];
[mBT release];
}
}
//--PROBLEM I HAVE IS BELOW--//
-(void)addEnemy {
int x = (arc4random() % 3);
TBT *tBT = (TBT*)
[sceneSpriteBatchNode getChildByTag:kEnemyType1BT];
//--Just using one object(sprite) to begin with--//
if (x>0) {
CCLOG(#"RANDOM KSTATETEST!!!!!!");
[tBT changeState:kStatetest]; <---it is not changing state to kStatetest
}
-(id)init {
self = [super init];
if (self !=nil) {
CGSize screenSize = [CCDirector sharedDirector]. winSize;
self.TouchEnabled = YES;
srandom(time(NULL));
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:#"scene1atlas.plist"]; // 1
sceneSpriteBatchNode =
[CCSpriteBatchNode batchNodeWithFile:#"scene1atlas.png"]; // 2
} else {
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:#"scene1atlasiPhone.plist"]; // 1
sceneSpriteBatchNode =
[CCSpriteBatchNode batchNodeWithFile:#"scene1atlasiPhone.png"];// 2
}
[self addChild:sceneSpriteBatchNode z:0]; // 3
[self initJoystickAndButtons]; // 4
BC *viking = [[BC alloc]
initWithSpriteFrame:[[CCSpriteFrameCache
sharedSpriteFrameCache]
spriteFrameByName:#"BCmoving_anim_1.png"]];
//[viking setJoystick:leftJoystick];
[viking setJumpButton:jumpButton];
[viking setAttackButton:attackButton];
[viking setPosition:ccp(screenSize.width * 0.19f,
screenSize.height * 0.19f)];
[viking setCharacterHealth:3];
[sceneSpriteBatchNode
addChild:viking
z:kVikingSpriteZValue
tag:kVikingSpriteTagValue]; heatlh is set to 100
[self schedule:#selector(addEnemy) interval:1.0f];
[self createObjectOfType:kEnemyType1BT withHealth:3 atLocation:ccp(screenSize.width * 0.0439f, screenSize.height * 0.822f) withZValue:10];
[self createObjectOfType:kEnemyType3BT withHealth:3 atLocation:ccp(screenSize.width * 0.0439f, screenSize.height * 0.45f) withZValue:10];
[self createObjectOfType:kEnemyType2BT withHealth:3 atLocation:ccp(screenSize.width * 0.0439f, screenSize.height * 0.638f) withZValue:10];
//Sets up the schedular call that will fire the update method in GamePlayLayer.m every frame.
[self scheduleUpdate];
}
return self;
}
#end
//----------------- below is one of my objects header files--------------//
#import <Foundation/Foundation.h>
#import "GameCharacter.h"
#interface TBT : GameCharacter{
CCAnimation *tiltingAnim;
CCAnimation *transmittingAnim;
CCAnimation *loseLifeAnim;
CCAnimation *throwingAnim;
CCAnimation *afterThrowingAnim;
CCAnimation *shootPhaserAnim;
GameCharacter *vikingCharacter;
id <GamePlayLayerDelegate> delegate;
}
#property (nonatomic,assign) id <GamePlayLayerDelegate> delegate;
#property (nonatomic, retain) CCAnimation *tiltingAnim;
#property (nonatomic, retain) CCAnimation *transmittingAnim;
//#property (nonatomic, retain) CCAnimation *takingAHitAnim;
#property (nonatomic, retain) CCAnimation *loseLifeAnim;
#property (nonatomic, retain) CCAnimation *throwingAnim;
#property (nonatomic,retain) CCAnimation *afterThrowingAnim;
#property (nonatomic,retain) CCAnimation *shootPhaserAnim;
-(void)initAnimations;
#end
//-----------------below is the .m file for one of my objects--------------//
#import "TBT.h"
#implementation TBT
#synthesize delegate;
#synthesize tiltingAnim;
#synthesize transmittingAnim;
#synthesize loseLifeAnim;
#synthesize throwingAnim;
#synthesize afterThrowingAnim;
#synthesize shootPhaserAnim;
-(void) dealloc {
delegate = nil;
[tiltingAnim release];
[transmittingAnim release];
[loseLifeAnim release];
[throwingAnim release];
[afterThrowingAnim release];
[shootPhaserAnim release];
[super dealloc];
}
-(void)shootPhaser {
CGPoint phaserFiringPosition;
PhaserDirection phaserDir;
CGRect boundingBox = [self boundingBox];
CGPoint position = [self position];
float xPosition = position.x + boundingBox.size.width * 0.542f;
float yPosition = position.y + boundingBox.size.height * 0.25f;
if ([self flipX]) {
CCLOG(#"TBT Facing right, Firing to the right");
phaserDir = kDirectionRight;
} else {
CCLOG(#"TBT Facing left, Firing to the left");
xPosition = xPosition * -1.0f;
phaserDir = kDirectionLeft;
}
phaserFiringPosition = ccp(xPosition, yPosition);
[delegate createPhaserWithDirection:phaserDir andPosition:phaserFiringPosition];
}
-(void)changeState:(CharacterStates)newState {
[self stopAllActions];
id action = nil;
[self setCharacterState:newState];
switch (newState) {
case kStatespawning:
CCLOG(#"TBT->Changing State to Spwaning");
[self setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:#"BT_anim_1.png"]];
break;
case kStateIdle:
CCLOG(#"TBT->schaning state to idle");
[self setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:#"BT_anim_1.png"]];
break;
case kStatetest:
CCLOG(#"TBT->Changing State to test");
action = [CCSequence actions : [CCDelayTime actionWithDuration:1.5f],[CCAnimate actionWithAnimation:transmittingAnim], nil];
break;
default:
CCLOG(#"unhandled state %d in TBT", newState);
break;
}
if (action !=nil) {
[self runAction:action];
}
}
-(void)updateStateWithDeltaTime: (ccTime)deltaTime andListOfGameObjects:(CCArray*)listOfGameObjects {
if (characterState == kStateDead)
return;
if ((([self numberOfRunningActions] == 0) && (characterState != kStateDead)) ) {
CCLOG(#"TBT Going to Idle");
[self changeState:kStateIdle];
return;
}
}
-(void)initAnimations {
[self setTiltingAnim:[self loadPlistForAnimationWithName:#"tiltingAnim" andClassName:NSStringFromClass([self class])]];
[self setTransmittingAnim:[self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
}
-(id) initWithSpriteFrameName:(NSString*)frameName{
if ((self=[super init])) {
if ((self = [super initWithSpriteFrameName:frameName])) {
CCLOG(#"### TBT initialized");
[self initAnimations];
characterHealth = 3.0f;
gameObjectType = kEnemyType1BT;
[self changeState:kStatespawning];
}}
return self;
}
#end
Finally found out that all I needed was to add the following code to my TBT gameCharacters-(void)updateStateWithDeltaTime: method...
if (characterState == kStateTest) {
if (characterState != kStateTest) {
[self changeState:kStateTest];
return;
}
now it works fine.
Thanks again to Mikael for trying to help me.
I'm creating a CGRect within a class (Path). I've verified that the the class is creating the rectangle. Now, I've built a method into the class that should, in theory, return true when asked if a touch location is within the rectangle. Problem is, it always returns false. I've verified that the method is getting the touch, as needed. Relevant code below:
Path.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Path : CCSprite {
CGPoint startP;
CGPoint endP;
CGRect pathRect;
CGPoint touchP;
}
-(id)initWithPoints:(CGPoint)sP :(CGPoint)eP;
-(bool)touchWithinBounds:(CGPoint)touch;
#property (nonatomic, assign) CGPoint startP;
#property (nonatomic, assign) CGPoint endP;
#property (nonatomic, assign) CGRect pathRect;
#end
Path.mm
#import "Path.h"
#implementation Path
#synthesize startP;
#synthesize endP;
#synthesize pathRect;
-(id) initWithPoints:(CGPoint)sP :(CGPoint)eP {
if ((self = [super init])) {
startP = sP;
endP = eP;
pathRect = CGRectMake(startP.x-2, endP.y, 5, 480);
}
return self;
}
-(bool)touchWithinBounds:(CGPoint)touch {
touchP = touch;
if(CGRectContainsPoint([self pathRect], touch)) {
return true;
} else {
return false;
}
}
-(void) draw {
glColor4f(1.0f,1.0f,1.0f,1.0f);
glLineWidth(5.0f);
ccDrawLine(startP, endP);
}
-(void) dealloc {
[super dealloc];
}
#end
In my main game scene, I initialize a path and add it to a mutable array:
paths = [[NSMutableArray alloc] init];
Path *path1 = [[[Path alloc] initWithPoints:ccp(winSize.width/3, 0.0) :ccp(winSize.width/3, winSize.height)] autorelease];
[self addChild:path1 z:0];
[paths addObject:path1];
And then in ccTouchesBegan, I send the touch location to touchWithinBounds. But instead of reporting true when the rectangle is touched, it always reports false:
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
for (Path *path in paths) {
CCLOG(#"%d", [path touchWithinBounds:location]);
Is there some kind of connection I'm missing between the pieces, or am I misunderstanding how the interaction works all-together?
Thanks
Figured it out. I had the points I was originating my rectangle at wrong.
I tried to include a custom button programmatically inside a UIView which is a subview of UITabBarController.
The button is displays fine but when I click it, it crashes without an error message. Its strange that sometimes it does inconsistently:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString playButton]: unrecognized selector sent to instance 0x632cd10'
I even tried removing any code in my play button method, i tried changing the name playButton to playAction, I also tried add the button directly to "self" not to aboutView but the result is still the same.
My guess is it got something to do with the tabBar having a UIView as a subview with a button on it. I don't know.
Here's a code snippet of how i built the tabBar in my appDelegate method
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutNavC = [[[UIViewController alloc] init] autorelease];
aboutNavC.title = #"About";
aboutNavC.view = aboutViewC.view;
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];
and heres the code for implementing the AboutViewController class
AboutViewController.h
#import
#interface AboutViewController : UIViewController {
UIButton *playSoundButton;
UIView *aboutView;
}
- (void)playButton;
#end
AboutViewController.m
#import "AboutViewController.h"
#implementation AboutViewController
- (void)dealloc {
[playSoundButton release];
[aboutView release];
[super dealloc];
}
- (void)viewDidLoad {
aboutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain];
image = [UIImage imageNamed:#"bt_play.png"];
[playSoundButton setImage:image forState:UIControlStateNormal];
[playSoundButton addTarget:self action:#selector(playButton) forControlEvents:UIControlEventTouchUpInside];
playSoundButton.frame = CGRectMake(0, 350, 40, 40);
[aboutView addSubview:playSoundButton];
stopSoundButton.hidden = YES;
playSoundButton.hidden = NO;
[self.view addSubview:aboutView];
[super viewDidLoad];
}
- (void)playButton
{
NSLog(#"playAction method");
}
#end
Thanks in advance!
[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain]; should read playSoundButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
(i.e. move the first [ further right to UIButton)
Thanks tob for the feedback, i actually know how to implement retain statement, it was a typo i didn't notice. surprisingly, that is not the problem and the code run's fine now leaving the typo unchanged.
The real problem was in my appDelegate method.
Instead of
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutNavC = [[[UIViewController alloc] init] autorelease];
aboutNavC.title = #"About";
aboutNavC.view = aboutViewC.view;
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];
should be
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutViewC.title = #"About";
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutViewC, lessonsNavC, nil];