Im getting This Error Instance method '-setTouchEnabled:' not found (return type defaults to 'id')
I don't know whats causing it, any help would be greatly appreciated :)
Heres my code sorry if its messy
- (id) init
{
if ((self = [super init])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *player = [CCSprite spriteWithFile:#"start.png"];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
[self schedule:#selector(gameLogic:) interval:0.5];
[self setTouchEnabled:YES];
_monsters = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
// stuff that needs updating
[self schedule:#selector(update:)];
//labels
StrategyBullet = 10;
Strategyscore = 0;
CCLabelTTF *label2 = [CCLabelTTF labelWithString:#"Ninja stars " fontName:#"Arial" fontSize:15];
label2.position = ccp(400,310);
label2.color = ccc3(255, 0, 0);
[self addChild: label2 z:1];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Score" fontName:#"Arial" fontSize:15];
label.position = ccp(30,310);
label.color = ccc3(255, 0, 0);
[self addChild: label z:1];
StrategyscoreLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
StrategyscoreLabel.position = ccp(65, 310);
StrategyscoreLabel.color = ccc3(255, 255, 255);
[self addChild:StrategyscoreLabel z:1];
StrategyBulletLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
StrategyBulletLabel.position = ccp(450, 310);
StrategyBulletLabel.color = ccc3(255, 255, 255);
[self addChild:StrategyBulletLabel z:1];
}
return self;
}
The setter you're looking for would look like this.-
[self setIsTouchEnabled:YES];
(notice the missing 'Is'). Or you can directly write
self.isTouchEnabled = YES;
EDIT
As #Grzegorz Krukowski said, setIsTouchEnabled is currently deprecated and setTouchEnabled is actually the method used in cocos2d 2.x. My guess is that you may be using a version prior 2.x.
Related
I can't seem to figure out why the bad access code appears, can someone help?
I have code to add a sprite and an emitter node, each of which have their own physics bodies. Now, the sprite moves around the map, and there is a camera that follows it. The sprite's parent is myWorld, which itself is a child of the scene. When I attempt to add a physicsJoint between the moving sprite (which is not moving when the joint is initialized), I get a bad access code. Here is the code:
self.sprite = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
self.sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self.sprite setScale:.5];
self.sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.sprite.size];
self.sprite.physicsBody.mass = 50;
self.sprite.physicsBody.affectedByGravity = NO;
self.sprite.name = #"sprite";
self.sprite.zPosition = 5;
self.sprite.physicsBody.velocity = CGVectorMake(300, 0);
self.sprite.physicsBody.categoryBitMask = plane;
self.sprite.physicsBody.collisionBitMask = ships;
self.sprite.physicsBody.affectedByGravity = YES;
NSString *firePath = [[NSBundle mainBundle] pathForResource:#"MyParticle" ofType:#"sks"];
self.smokeTrail = [SKEmitterNode node];
self.smokeTrail = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath];
self.smokeTrail.position = CGPointMake(self.sprite.position.x-2*self.sprite.size.width/5, self.sprite.position.y);
[self.smokeTrail setScale:1];
self.smokeTrail.zPosition = self.sprite.zPosition-1;
self.smokeTrail.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.smokeTrail.frame.size];
self.smokeTrail.physicsBody.dynamic = YES;
self.smokeTrail.physicsBody.affectedByGravity = NO;
self.smokeTrail.physicsBody.categoryBitMask = 0;
smokeJoint = [SKPhysicsJointFixed jointWithBodyA:self.sprite.physicsBody bodyB:self.smokeTrail.physicsBody anchor:CGPointMake(self.smokeTrail.position.x+self.smokeTrail.frame.size.width/2,self.smokeTrail.position.y)];
...
[myWorld setScale:0.4f];
self.sprite.physicsBody.velocity = CGVectorMake(0, 0);
[self addChild:myWorld];
[self addChild:backgroundNode];
[self addChild:buttonDown];
[self addChild:buttonUp];
[myWorld addChild:blueBox];
[self addChild:self.speed];
[self addChild:self.altitude];
[self addChild:self.darkBlueWave];
[self addChild:self.lightBlueWave];
[myWorld addChild:self.smokeTrail];
[myWorld addChild:self.sprite];
[myWorld addChild:randomNode];
[myWorld addChild:camera];
[self.physicsWorld addJoint:smokeJoint]; // Bad Access Code Happens Here
I want my character to move to the right by force. But it is not working on this code below. I don't know what I missed here, but the player stands still on the bottom without any movement. Any clue?
// MyScene.h
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
[self.player.physicsBody applyForce: CGVectorMake(100, 100)];
}
-(void)createSceneContents {
self.currentBackground = [Background generateBackground];
[self addChild: self.currentBackground];
self.physicsWorld.gravity = CGVectorMake(0, _gravity);
self.physicsWorld.contactDelegate = self;
Player *player = [[Player alloc]init];
player.size = CGSizeMake(80, 70);
player.position = CGPointMake([UIScreen mainScreen].applicationFrame.size.width/2, 40);
[self addChild:player];
}
//Player.h
#import "Player.h"
#import "common.h"
#implementation Player
- (instancetype)init {
self = [super initWithImageNamed:#"player.png"];
self.name = #"player";
NSLog(#"%f %f", self.size.width, self.size.height);
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 70)];
self.physicsBody.dynamic = YES;
self.physicsBody.allowsRotation = NO;
self.physicsBody.affectedByGravity = YES;
self.physicsBody.collisionBitMask = ~poopCategory & groundCategory;
self.physicsBody.categoryBitMask = playerCategory;
self.physicsBody.contactTestBitMask = poopCategory;
self.zPosition = 100;
//self.anchorPoint = CGPointMake(0.5, 0);
return self;
}
It looks like you are using a property named player in the scene, but you are initializing a sprite named player but never storing it in the scene's property. NSLog self.player and you'll see it's nil.
I try to add CCScrollView with paging (cocos2d- iphone v3.0). But it's not working.
It doesn't call any delegate methods (for example scrollViewDidScroll:).
CCNode *cards = [CCNode node];
for (int i = 0 ; i < 3; i++) {
CCLabelTTF *label = [CCLabelTTF labelWithString:[NSString stringWithFormat:#"label %d", i] fontName:#"Arial" fontSize:24];
label.color = [CCColor redColor];
label.position = ccp(winSize.width * i + winSize.width * 0.5 , winSize.height * 0.5);
[cards addChild:label];
}
self.scrollView = [[CCScrollView alloc] initWithContentNode:cards];
self.scrollView.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentSize = CGSizeMake(3, 1);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.position = CGPointZero;
self.scrollView.anchorPoint = CGPointZero;
[self addChild:self.scrollView];
You actually need to set the contentSize of the contentNode of the scrollView as opposed to the contentSize of the scrollView.
In CCScrollView.h
#property (nonatomic,strong) CCNode* contentNode;
So you should replace this part of the code:
self.scrollView.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentSize = CGSizeMake(3, 1);
With this:
self.scrollView.contentNode.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentNode.contentSize = CGSizeMake(3, 1);
I have tried my best to boil this question down as simple as possible. I have a coin object in my game:
#implementation
-(CollectableCoin*)initWithLocation:(CGPoint) Location andValue: (int) val
{
self = [super initWithImageNamed:#"coin"];
[self setScale:.35];
_value = val;
_collected = false;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = APAColliderTypeCoin;
self.physicsBody.collisionBitMask = APAColliderTypeBall;
self.physicsBody.mass = 0.00009;
self.physicsBody.restitution = .35;
self.position = Location;
self.name = #"collectableCoin";
return self;
}
#end
I also have a shelf object:
#implementation Shelf
-(Shelf*)initWithLocation:(CGPoint) location andWidth:(NSInteger) width
{
self = [super initWithImageNamed:#"shelf"];
if(self)
{
self.size = CGSizeMake(width, HEIGHT);
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.dynamic = false;
self.physicsBody.restitution = 0;
self.position = location;
self.name = #"shelf";
SKSpriteNode* topOfShelf;
if(width > 5)
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width-2, 1)];
else
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width, 1)];
topOfShelf.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:topOfShelf.size];
topOfShelf.physicsBody.restitution = 1;
topOfShelf.physicsBody.dynamic = false;
topOfShelf.position = CGPointMake(0, location.y + self.size.height/2);
NSLog([NSString stringWithFormat:#"%f", location.y + self.size.height/2]);
NSLog([NSString stringWithFormat:#"%f", location.y]);
topOfShelf.name = #"shelf";
[self addChild:topOfShelf];
}
return self;
}
#end
I create a scene like so:
-(id)initWithSizeTest:(CGSize)size
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
_gameState = READYTOSTART;
self.physicsWorld.contactDelegate = self;
if (self = [super initWithSize:size])
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
for(int i = 0; i < 25; i++)
{
CollectableCoin* orb = [[CollectableCoin alloc] initWithLocation:CGPointMake(i*10, self.size.height*.75) andValue:1];
[self addChild:orb];
}
Shelf* shelf = [[Shelf alloc] initWithLocation:CGPointMake(self.size.width/2, self.size.height/2) andWidth:self.size.width];
[self addChild:shelf];
}
return self;
}
Here is the touchesBegan method:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if(_gameState == READYTOSTART)
{
self.physicsWorld.gravity = CGVectorMake(0, -2.0);
_gameState = PLAYING;
[[self childNodeWithName:#"taptostart"] removeFromParent];
}
When the scene starts, I have a row of coins hovering above a shelf, gravity is disabled, and I have a solid 60fps. When I tap the screen, the touchesbegan function enables gravity and the coins fall on the shelf, and the frame rate drops to 5fps. The didBeginContact function is not being called because the shelf object is not dynamic nor does it have contact or collision bitmasks, so I am fairly sure that it is not being overloaded by extraneous calls to didBeginContact. This happens on an iPad mini and an iPhone 4s, but not in any of the simulators. This is a very simple example of the actual problem that I am having. Anyone have any insights?
I don't know why but my animation does not work.
I am trying to add an animated unit now via sprite sheet but through a set of sprites.
And something goes wrong
Here is the code of init method:
#implementation Enemy
-(id) initAt:(CGPoint)pos
{
if([super init])
{
self.position = pos;
CCSprite *start_sprite = [CCSprite spriteWithFile:#"unit1_00000.png"];
start_sprite.position = ccp(0,0);
[self addChild:start_sprite z:2];
const int FRAMES_COUNT = 10;
NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:FRAMES_COUNT];
for (int i = 0; i < FRAMES_COUNT; i++)
{
NSString* file = [NSString stringWithFormat:#"unit1_0000%i.png", i];
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
[frames addObject:frame];
}
_default_animation = [CCAnimation animationWithSpriteFrames:frames delay:0.1f];
_current_anim_action = [CCAnimate actionWithAnimation:_default_animation];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:_current_anim_action];
[self runAction:repeat];
}
return self;
}
The error is the following: -[Enemy setDisplayFrame:]: unrecognized selector sent to instance 0x8929bd0
setDisplayFrame is a method of CCSprite class, so in order to run a CCAnimation on Enemy class (which internally calls setDisplayFrame), Enemy must extend CCSprite, not CCNode.