I have the following map I made in Tiled, and then added to my project:
map = [[CCTMXTiledMap alloc] initWithTMXFile:#"FirstLevel.tmx"];
[self addChild:map];
I also have the following object which contains the player that I can move around:
objectLayer = [CCSpriteBatchNode batchNodeWithFile:#"level.pvr.ccz" capacity:150];
[self addChild:objectLayer z:10];
player = [[[Player alloc] initWithGameLayer:self] autorelease];
[objectLayer addChild:[player ccNode] z:10000];
[player setPhysicsPosition:b2Vec2FromCC(240,150)];
I can see my tilemap in the background, but it doesn't collide with anything. How can I make Player colide with the layers in my Tilemap?
Did it with Rubymotion and Joybox http://joybox.io/
Related
I can't figure out how to handle with tile map layer and other node. I want to make another one layer(CCNode) for such things as menu button, score, joystick that must always stay fixed(not scrolled), when the whole map is scrolled.
self.theMap=[CCTiledMap tiledMapWithFile:#"map.tmx"];
self.bgLayer=[theMap layerNamed:#"bg"];
[self addChild:theMap z:-1];
CCNode *joystickNode;
joystickNode=[CCNode node];
[bgLayer.parent addChild:joystickNode z:2];
upArrowFrame=[CCSpriteFrame frameWithImageNamed:#"uparrow.png"];
upArrow=[CCButton buttonWithTitle:#"" spriteFrame:upArrowFrame];
[upArrow setTarget:self selector:#selector(upArrowPressed)];
upArrow.position= ccp(190,190);
[joystickNode addChild:upArrow z:2];
Now upArrow is not visible on the screen at all. If I add to self instead of joystickNode, it will appear.
I can't even understand, what parent should new CCNode have. Can anyone explain it to me? I also tried to add new CCNode as a child of self and theMap.
EDIT: Oops, it's actually moving the camera. How to implement it in this case?
-(void)setCenterOfScreen :(CGPoint) position {
CGSize screenSize=[[CCDirector sharedDirector]viewSize];
int x=MAX(position.x, screenSize.width/2);
int y=MAX(position.y, screenSize.height/2);
x= MIN(x, theMap.mapSize.width * theMap.tileSize.width - screenSize.width/2);
y= MIN(y, theMap.mapSize.height * theMap.tileSize.height - screenSize.height/2);
CGPoint goodPoint =ccp(x,y);
CGPoint centerOfScreen = ccp(screenSize.width/2, screenSize.height/2);
CGPoint difference = ccpSub(centerOfScreen, goodPoint);
self.position=difference;
}
- (void)update:(CCTime)dt{
[self setCenterOfScreen:hero.position];
}
I'm having a strange problem... I have a custom class with sprite and booleans properties
In my main GameLayer, I have CCLayerColor *my1layer and in my .m section I have this code:
my1layer = [CCLayerColor layerWithColor:ccc4(144,238,144, 255)];
[self addChild: my1layer z:5 tag:100];
[my1layer setContentSize:CGSizeMake(200, 1280)];`
//default start position
my1layer =ccp(s.width/2-420, s.height-s.height+180.);
MylmageItem *postImage1 = [[MylmageItem alloc] initWithLayer:self];
[postImage1 release];
postImage1.postImageNum=CCRANDOM_0_1()*12 +1;
postImage1.postImage = [CCSprite spriteWithSpriteFrameName:(NSString*)[self assignPostImage:postImage1.postImageNum ]];
postImage1.postImage.position =ccp(my1layer.contentSize.width/2, my1layer.contentSize.height-my1layer.contentSize.height+80);
[my1layer addChild:myImage1.slotImage z:6 tag:101];’
code above does work and shows the images on layer, however when I try to access the property postImage1.postImageNum, it shows 0 always...
I do this: CCLayer *myLayer1 = (CCLayer*)[self getChildByTag:100]; later to retrieve my object...
MyImageItem *tmpImage1 = (MymageItem*)[myLayer1 getChildByTag:101]; from here I always get 0 when I do CCLOG(#"postImage1.postImageNum %d",tmpImage1.postImageNum);
How do I get data from my1layer?
I have 4 sprites, every time the game starts it would randomly choose 1 from the 4 sprites to be the main sprite.
How can I do this?
I know I would need to use arc4random
The easiest way would be -- First name you image (.png) files with some numbers for e.g. sprite1.png, sprite2.png ....
int rndSprtNum = (arc4random() % 4) + 1;
CCSPrite *mainSprite = [CCSprite spriteWithFile:[NSString StringWithFormat:#"sprite%d.png",rndSprtNum]];
mainSprite.position = ccp(x,y);
[self addChild:mainSprite];
This way you no need to take a mutable array etc. Hope this helps.
First of all you are add all sprite in NSMutableArray as below code.
Allocate Array
NSMutableArray *AryT = [[NSMutableArray alloc]init];
Differnt sprite
CCSprite *torpedoOne1 = [CCSprite spriteWithFile:#"A#2x.png"];
CCSprite *torpedoOne2 = [CCSprite spriteWithFile:#"B#2x.png"];
CCSprite *torpedoOne3 = [CCSprite spriteWithFile:#"C#2x.png"];
Add All This sprite in define Array
[AryT addObject:torpedoOne1];
[AryT addObject:torpedoOne2];
[AryT addObject:torpedoOne3];
Take Random number from array
int RandomIndex = arc4random_uniform(AryT.count);
Randomaly sprite
[self addchild:[AryT objectAtIndesx:RandomIndex]];
Here's another approach, in case you don't want to rename your files you can simply put their names in an array:
NSString *names[4] = {#"RedSprite.png", #"GreenSprite.png", #"YellowSprite.png", #"PurpleSprite.png"};
CCSprite *sprite = [CCSprite spriteWithFile:names[arc4random_uniform(4)]];
[self addChild:sprite];
How To Have The Same Sprite In Multiple Locations Cocos2d Please Help
I have searched all over and cannot find answer
Just create multiple Sprites (CCSprite instances). They can all use the same texture (bitmap-file).
CCSprite * mySprite1;
CCSprite * mySprite2;
CCSprite * mySprite3;
// create several sprites from the same bitmap file
mySprite1 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite2 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite3 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite1.position = ccp(100, 100);
mySprite2.position = ccp(200, 200);
mySprite3.position = ccp(300, 300);
You can not add the same CCSprite as a child to multiple CCNodes but you can make Cocos2D render the same CCSprite multiple times.
To achieve this you need to create a subclass of CCNode that will store the reference to your CCSprite and draw it in its -draw method applying required transformations.
For example
-(void)draw
{
[super draw];
CGPoint initialPosition = [_node position];
float initialScale = [_node scale];
[_node setScale:self.scale];
[_node setPosition:self.position];
[_node visit];
[_node setPosition:initialPosition];
[_node setScale:initialScale];
}
You may have to to use glScissor if you need picture-in-picture appearance.
Then you just need to addChild an instance of this class for every time you want an additional copy of your original CCSprite rendered.
Put a method on a for loop.
Inside the method create the CCSprite and modify it.
This is best suited for static sprites, since I don't know how you would access these outside of the method.
I've got to sprites, essentially a nucleus (parent) and an electron (child). I'm trying to find a way to have the electron orbit the nucleus. I've found a few posts here and there on moving anchor points, but that is apparently related to the texture of the sprite, not the sprite itself.
This is my current init method for my parent sprite:
self = [super init];
if (self) {
CCSprite *orbitAnchor = [CCSprite new];
[self addChild:orbitAnchor];
orbitAnchor.position = ccp(32,32);
CCSprite *orbiter = [CCSprite spriteWithFile:#"line.png" rect:CGRectMake(0, 0, 8, 8)];
[orbitAnchor addChild:orbiter];
orbiter.position = ccp(40,40);
CCAction *orbitAction = [CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:1 angle:720]];
[orbitAnchor runAction:orbitAction];
[self initAnimations];
}
return self;
Those numbers are all totally arbitrary though - I just stuck them in there and got what looked best. I'm sure there's a more programatic way to do what I want.
Basically, I'm looking for a way to set the child's axis point at the center of the parent, and then have it rotate around that point. It seems like a relatively simple thing to do using CCRotate and such, but I think I'm missing what to search for in order to move the anchor point of the child. Any suggestions/other posts?
Thanks
You have [CCSprite new] which is an unusual use, probably not supported. Unless the orbit anchor node should display a texture, you can just use a CCNode as anchor.
Technically you're doing everything correct from what I can see. You might want to try without the rotate actions and instead change the direction manually in a scheduled update method.
CCNode *orbitAnchor = [CCNode node];
[self addChild:orbitAnchor z:0 tag:1];
orbitAnchor.position = ccp(32,32);
CCSprite *orbiter = [CCSprite spriteWithFile:#"line.png" rect:CGRectMake(0, 0, 8, 8)];
[orbitAnchor addChild:orbiter z:0 tag:2];
orbiter.position = ccp(40,40);
[self scheduleUpdate];
Update method:
-(void) update:(ccTime)delta
{
CCNode* orbitAnchor = [self getChildByTag:1];
orbitAnchor.direction += 5;
}
From the image filename it looks like you're trying to draw a line from the orbitAnchor to the orbiter. You can do that with ccDrawLine:
-(void) draw
{
CCNode* orbitAnchor = [self getChildByTag:1];
CCNode* orbiter = [self getChildByTag:2];
ccDrawLine(orbitAnchor.position, orbiter.position);
}
Anchor points. Automatically, objects are placed based on the center of an object right? with anchor points you can move that around, so if you rotate the object it will rotate around the anchorpoint. http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/