I have a tile map and when I run it on my simulator, it shows up fine. When I run it on my iPhone and go to the map, nothing shows up, only a black screen. any ideas on what is happening?
#interface LevelDesertOne : CCLayer {
CCTMXTiledMap *desertMap;
CCTMXLayer *bgLayerDesert;
}
#property(nonatomic, retain) CCTMXTiledMap #desertMap;
#property(nonatomic, retain) CCTMXLayer *bgLayerDesert;
+(CCScene *) scene;
#end
#implementation LevelDesertOne
#synthesize desertMap;
#synthesize bgLevelDesert;
+(CCScene *) scene{
CCScene *scene = [CCScene node];
LevelDesertOne *layer = [LevelDesertOne node];
[scene addChild: layer];
return scene;
}
-(id) init{
if( (self=[superinit]) ){
self.desertMap = [CCTMXTiledMap tiledMapWithTMXFile:#"Desert.tmx"];
self.bgLayerDesert = [desertMap layerNamed:#"bgDesert"];
[self addChild:desertMap z:-1];
}
return self;
}
#end
Copy and paste, please don't ad-lib. Your interface and implementation names do not match, synthesize statement for non-matching objects, etc. My guess is that your tmx file has different casing, and you've made a typo. In the simulator, it wouldn't matter if your file is actually named Desert.tmx . You could call it DeSeRt.TmX and it would still run, but on the actual device it would not. Default for OSX is case-insensitive filesystem, default for iOS is case-sensitive.
Check to make sure that your tile map is added to the project and being copied into the resources folder. Assuming that this is the case, it may also be that the file you are loading #"Desert.tmx" is a different upper/lowercase than the actual file you're linking. (MacOS filesystem is by default not case sensitive, iOS filesystem is)
Related
I'm currently trying out a few things. I already did this tutorial: https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/
However I'm a little stuck now.
I created a CCNode in SpriteBuilder and added 3 images. carbody.png and 2 wheel.png.
I made all objects physicsObjects.
In code I tried to connect them with joints and move them. However the carBody moves but the wheels keep staying on their place.
#import "Car.h"
#implementation Skater{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;
}
- (id)init {
self = [super init];
if (self) {
[_wheel1.physicsBody setCollisionGroup: _carBody];
[_wheel2.physicsBody setCollisionGroup: _carBody;
[_carBody.physicsBody setCollisionGroup: _carBody];
_bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
_bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];
}
return self;
}
And in Gameplay.m I only did this:
-(void)didLoadFromCCB{
[_car runAction:
[CCActionMoveTo actionWithDuration:10 position:CGPointMake(2000,_car.position.y)]];
}
The carBody moves, the wheels don't...
What am I missing?
Edit:
I made a few changes now but my wheels still keep staying on their place...
#import "Car.h"
#implementation Car{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;
}
- (id)init {
self = [super init];
if (self) {
CCLOG(#"Car created");
[_wheel1.physicsBody setCollisionGroup:_carBody];
[_wheel2.physicsBody setCollisionGroup:_carBody];
[_carBody.physicsBody setCollisionGroup:_carBody];
_bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
_bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];
}
return self;
}
-(void)moveCar:(int)distance{
CCLOG(#"Car should move");
CGPoint launchDirection = ccp(1, 0);
CGPoint force = ccpMult(launchDirection, distance);
[self.physicsBody applyForce:force];
}
#end
You're simply missing the point that move (and other) actions bypass physics. Once a node has a physics body, the use of CCAction* classes that change position and rotation is a no-no, and most others (ie scale) will not be applied to the physics body either but can still be used safely.
To move a physics object, apply an impulse or force to the physics body.
I'm following "Learn Cocos2D" and in chapter 4 I'm met with the following directive:
And in the GameLayer init method, add the call to the initSpiders method discussed next, right after scheduleUpdate:
-(id) init {
if ((self=[super init])) {
...
[self scheduleUpdate];
[self initSpiders];
}
return self;
}
I get and ARC error message: no visible #interface for 'GameLayer' declares the selector 'initSpiders'
I get the same message at the line: self resetSpiders
what am i missing? everything builds and runs great up to that point.
This issue derives from the fact that the initSpiders and resetSpiders are not declared in your class interface and are defined in the .m file after the point where they are used.
If they are not missing altogether, you can fix this in either of 2 ways:
move the definition of the initSpiders and resetSpiders methods above your init method and the errors will disappear;
add a declaration for both methods in the #interface of the class.
(If you do both, it will also work)
Check your code to see if the implementation for those methods is available.
Your error appears to be that you haven't followed the next bit of the book too. Completing the next section should allow you to compile your code without warnings like this.
A more complete extract of that section of the book is:
And in the GameScene init method add the call to the initSpiders method discussed next, right after scheduleUpdate:
-(id) init {
if ((self = [super init]))
{
… 96 CHAPTER 4: Your First Game
[self scheduleUpdate];
[self initSpiders];
}
return self;
}
After that a fair bit of code is added to the GameScene class, beginning with the initSpiders method in Listing 4–8, which is creating the spider sprites.
Listing 4–8. For Easier Access, Spider Sprites Are Initialized and Added to a CCArray
-(void) initSpiders
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
// using a temporary spider sprite is the easiest way to get the image's size
CCSprite* tempSpider = [CCSprite spriteWithFile:#"spider.png"];
float imageWidth = [tempSpider texture].contentSize.width;
// Use as many spiders as can fit next to each other over the whole screen width.
int numSpiders = screenSize.width / imageWidth;
// Initialize the spiders array using alloc.
spiders = [[CCArray alloc] initWithCapacity:numSpiders];
for (int i = 0; i < numSpiders; i++)
{
CCSprite* spider = [CCSprite spriteWithFile:#"spider.png"];
[self addChild:spider z:0 tag:2];
// Also add the spider to the spiders array.
[spiders addObject:spider];
}
// call the method to reposition all spiders
[self resetSpiders];
}
What I'm trying to achieve is the ability to use a Cocos2d SneakyInput SneakyJoystick to control the movement of my LHSprite (created using level helper) Character/Player in my box2d/cocos2d game.
I can get it to work, however, the sneakyJoystick joystick is in the same layer that my gameplay is in, and seen as though my game screen 'follows' the character - the joystick actually moves off the screen when the camera/screen is moved.
I have tried setting the joystick up in a separate layer ('MyUILayer'), and using it to control my character in my 'GameLayer'.
Here is how i've tried doing this:
in 'MyUILayer' i have the code to set up the following sneakyJoystick components:
#interface MyUILayer : CCLayer {
SneakyJoystick *leftJoystick;
SneakyButton * jumpButton;
SneakyButton * attackButton;
}
#property (readonly) SneakyButton *jumpButton;
#property (readonly) SneakyButton *attackButton;
#property (readonly) SneakyJoystick *leftJoystick;
Now, in 'GameLayer I tried to access the value created by the sneakyJoystick called 'leftJoystick' in 'MyUILayer'.
in the declaration file (GameLayer.h):
#import "MyUILayer.h"
#interface GameLayer : CCLayer {
//.............
LHSprite *character;
b2Body *characterBody;
SneakyJoystick *characterJoystick;
SneakyButton *jumpButton;
SneakyButton *attackButton;
//.............
}
in GameLayer.mm:
//in my INIT method
{
MyUILayer *UILAYER = [[MyUILayer alloc]init];
characterJoystick = UILAYER.leftJoystick;
[self scheduleUpdate];
// Define what 'character' is and what 'characterBody' is ('character is a LHSprite, and 'characterBody' is a b2Body)
}
//in my tick method
{
b2Vec2 force;
force.Set(characterJoystick.velocity.x * 10.0f, 0.0f);
characterBody->ApplyForce(force, characterBody->GetWorldCenter());
}
I really can't understand why 'characterBody', in 'GameLayer' won't move based on the value of 'leftJoystick', in 'MyUILayer'.
Sorry if its a bit long winded! - I have also uploaded my project file, so you can take a look at the project itself: https://dl.dropbox.com/u/2578642/ZOMPLETED%202.zip
Huge thanks to anyone who can help!
The problem lies in the way you are associating the MyUILayer with the GameLayer. On myscene.mm, you are creating a MyUILayer and a GameLayer, and adding them both to the scene. This is ok. But then, you make a NEW MyUILayer on GameLayer, and associate that joystick. You should associate the joystick in the MyScene.mm by using properties like so:
In MyScene.mm
// Control Layer
MyUILayer * controlLayer = [MyUILayer node];
[self addChild:controlLayer z:2 tag:2];
// Gameplay Layer
GameLayer *gameplayLayer = [GameLayer node];
gameplayLayer.attackButton = controlLayer.attackButton;
gameplayLayer.leftJoystick = controlLayer.leftJoystick;
gameplayLayer.jumpButton = controlLayer.jumpButton;
[self addChild:gameplayLayer z:1 tag:1];
In GameLayer.h add
#property (nonatomic, retain) SneakyButton *jumpButton;
#property (nonatomic, retain) SneakyButton *attackButton;
#property (nonatomic, retain) SneakyJoystick *leftJoystick;
In GameLayer.mm add
#synthesize jumpButton = jumpButton;
#synthesize attackButton = attackButton;
#synthesize leftJoystick = characterJoystick;
In GameLayer.mm, remove the UILAYER code in the init method
- (id)init
{
self = [super init];
if (self) {
//instalize physics
[self initPhysics];
[self lvlHelper];
[self characterLoad];
[self runAction:[CCFollow actionWithTarget:character worldBoundary:CGRectMake(0, -100, 870, 420)]];
[self scheduleUpdate];
}
return self;
}
Hey people,
I'm creating a game in cocos2d, (I'm very new to it, and was trying to solve this thing)
in the game I'm making I created a "Bomb" class, and a "Player" class,
I want the bomb to check for collision with the player, if a collision detected, explode.
My problem is that I have no idea how to get the player's position from the bomb class,
I'd be happy if you guys could help me out here,
Thanks!
You did add the CCSprites to a CCLayer, didn't you? Then that CCLayer should have the access to both of them. So, you can use the CCLayer's tick function to track the positions of the CCSprites and trigger actions if their bounding boxes overlap.
Some sample code to illustrate:
#interface MyLayer : CCLayer {
BombSprite *bomb;
PlayerSprite *player;
}
...
#end
#implementation MyLayer
- (id)init {
if ((self = [super init])) {
bomb = ...
player = ...
[self schedule:#selector(tick:)];
}
return self;
}
- (id)tick:(ccTime)dt {
if (CGRectContainsRect([bomb boundingBox], [player boundingBox])) {
NSLog(#"Collision!");
// call [player didCollideWith:bomb] or something
...
}
}
#end
There is a scene in my application which has only two labels and a menu item. When I load this scene using replaceScene method it stays for 3-4 seconds and then gets disappeared or released. I want to keep it until cancel button is pressed. How can I do it? code is:
#implementation MyLayer
+ (id)myScene {
CCScene *aScene = [CCScene node];
MYLayer *myLayer = [MyLayer node];
[aScene addChild:myLayer];
return aScene;
}
- (id) init {
if (self = [super init]) {
//labels and menu here
}
return self;
}
And I am calling it from another scene like this:
[[CCDirector sharedDirector] replaceScene: [MyLayer myScene]];
Maybe the problem is that it's your first scene. Then you should use runWithScene method of CCDirector.
did you try replacing that scene with a "empty" init function to see if it still releases itself? It might be because of the amount of textures you are putting into memory
I did have sort of similar problems before because the images used in the new scene is too big and got auto purged by my app delegate, thus returning me an empty scene sometimes