I'm having trouble inserting multiple children of same sprite and accessing it (or setting positions for them on runtime). Kindly advise any suitable method preferably point out my mistake. Here is my approach.
//In the Init Method...
//int i is defined in the start.
for (i = 1; i < 4; i++)
{
hurdle = [CCSprite spriteWithFile:#"hurdle1.png"];
[self addChild:hurdle z:i tag:i];
hurdle.position = CGPointMake(150 * i, 0);
}
It spreads all the sprites on the canvas. then in some "UPDATE Function" I'm calling this.
hurdle.position = CGPointMake(hurdle.position.x - 5, 10);
if (hurdle.position.x <= -5) {
hurdle.position = ccp(480, 10);
}
It works but as expected only one instance moves horizontally. I want all the instances to be moved so I am trying to use this....
for (i = 1; i < 4; i++){
[hurdle getChildByTag:i].position = CGPointMake(hurdle.position.x - 5, 10);
//OR
[hurdle getChildByTag:i].position = CGPointMake([hurdle getChildByTag:i].position.x - 5, 10);
}
I've tried getting LOGs on various places and realized that getChildByTag doesn't work the way I'm trying to use it.
The problem is in the last block of code. You should make a local reference to each CCSprite within your for loop.
Since you added the sprites to self, you will retrieve them as children of self
for (i = 1; i < 4; i++){
CCSprite * enumHurdle = [self getChildByTag:i];
enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}
Be careful if you create any other sprites this way in the same scene. It is bad design to give any two sprites the same tag.
EDIT about avoiding duplicate tags.
If you know how many sprites you will have. Use an enum of tags and refer to the sprites by name.
If not, knowing how many groups and putting a limit on the size of groups could make it managable.
ie
say you have 3 parts of code where you are generating sprites like this. You can include an enum in your .m (under #implementation line) and put the limits there
// Choose names that describe the groups of sprites
enum { kGroupOne = 0, // limiting the size of each group to 100
kGroupTwo = 100, // (besides the last group, but that is not important)
kGroupThree = 200,
};
Then when you create each group
// group 1
for (i = kGroupOne; i < 4; i++){
// set up code here
}
// group 2
// g2_size is made up, insert whatever you want
for (i = kGroupTwo; i < g2_size; i++) {
// set up code here
}
.
.
.
Then to retrieve in groups
for (i = kGroupOne; i < 4; i++){
CCSprite * enumHurdle = [self getChildByTag:i];
enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}
.
.
.
Hopefully that sparks your creativity. Now have some fun.
Something that I do often is group objects of like kind that I want to act on in a similar way by adding them to a CCNode and add that CCNode to the layer.
I would create a class that derives from CCNode
Then I can put all my logic in that node and access then via [self children]
for(CCSprite *hurdle in [self children]) {
// Do what you need to do
}
Related
I have a level select screen similar to candy crush's (a path of levels) where I plan to have more than 100 levels. When I load this many levels in one go, the time to create and load all of these levels is ~0.17 seconds. Now this is clearly much too long to wait after a user presses a button to go to the level. The game appears to freeze for a bit, then it finally goes.
Now I've been looking at a few different solutions to this. The one solution which I could revert to as a last resort is creating some sort of transition that temporarily shows a loading screen, then goes to the level select screen. It would be nice to bypass this loading all together though.
Currently my level select screen is a normal CCNode class with a scrollview on it. This means it is not a singleton, and every single time I go to it, a new instance loads. The nice thing about this is that I can constantly update whether or not someone has beaten a level, and change the color and ability to play the next level (is it possible to make this kind of update in a singleton class?). A singleton seems like the best approach to me (then I would just load in while the game loads at the beginning), but I'm not sure if the answer to my above question is yes. If someone has experience with this, please let me know!
I'm not really sure what else I could try to do. The only other good option seems to be somehow speeding up my code, but I'm not sure how I could do that (posted below). The last resort would be creating an instance of this class while my game loads, then just hide all user interaction on it whenever I don't want it to be showing. Idk though, that sounds a little sketch. Thanks for the advice, and here is my code creating the buttons.
-(void)didLoadFromCCB{
buttonArray = [NSMutableArray array];
CFTimeInterval startTime = CACurrentMediaTime();
CCSpriteFrame *redTile = [CCSpriteFrame frameWithImageNamed:#"Sprites/glossyTile.png"];
for (int l = 0; l < NUMBER_OF_WORLDS; l++) { //NUMBER_OF_WORLDS = 10
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++) {
NSString *tempTitle = [NSString stringWithFormat:#"%i",(k+1)+(l*5)];
tempButton = [CCButton buttonWithTitle:tempTitle
spriteFrame:redTile
highlightedSpriteFrame:redTile
disabledSpriteFrame:nil];
tempButton.block = ^(id sender) {
CCNode *transitionScene = [CCBReader load:#"TransitionScene"];
[self addChild:transitionScene];
int row = k+1;
int column = j+1;
int buttonIndex = (row + (column-1)*5) + l*20;
for (int m = 1; m < buttonIndex; m++) {
[[LevelManager sharedInstance] nextLevel];
}
levelNumber = i;
[self performSelector:#selector(changeScenes) withObject:nil afterDelay:8.0/30.0
];
};
tempButton.position = ccp(someConstant*j, someConstant2*k + someConstant3*l);
[self addChild:tempButton];
i++;
}
}
}
CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
CCLOG(#"time interval media %f", elapsedTime); //logs about 0.17 seconds
}
my script below does exactly what i need it to do, add blocks to a scene in any random order in the view. the only problem is, as i increase the amount of "block" nodes, they tend to overlap on one another and clump up, I'm wondering if there is a way i can add a "barrier" around each block node so that they cannot overlap but still give a random feel? My current code is below:
-(void) addBlocks:(int) count {
for (int i = 0; i< count; i++) {
SKSpriteNode *blocks = [SKSpriteNode spriteNodeWithImageNamed:#"Ball"];
blocks.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(blocks.size.width/2)];
blocks.physicsBody.dynamic = NO;
blocks.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:blocks];
int blockRandomPositionX = arc4random() % 290;
int blockRandomPositionY = arc4random() % 532;
blockRandomPositionY = blockRandomPositionY + 15;
blockRandomPositionX = blockRandomPositionX + 15;
blocks.position = CGPointMake(blockRandomPositionX, blockRandomPositionY);
}
}
Any help highly appreciated, thanks!
To prevent two nodes from overlapping, you should check the newly created node's random position with intersectsNode: to see if it overlaps any other nodes. You also have to add each successfully added node into an array against which you run the intersectsNode: check.
Look at the SKNode Class Reference for detailed information.
I'm making a simple SpriteKit game. It has one randomly generated world made up of lines. These lines are represented by the NHRLineNode class. I generate these lines all at once in the beginning of the level with two for loops, one for each side of the screen. This works fine. In addition to the main gameplay scene of the game, there is a "game over" screen that displays between plays and shows your score etc and a main menu scene. The problem comes when I play the game, die, see the game over screen, and play again. Looking at the memory usage in Xcode, it seems like the memory usage goes up when I first start the game from the menu scene, stays steady throughout the gameplay, and then jumps 6-10 MB when I die. This memory is never regained and the app uses more and more memory every time I play. I think this is because my for loops that generate the platforms are just creating a new instance of the NHRLineNode class, positioning it correctly, and then doing it again. Is this what is causing my memory issues? Or is it more likely something on the game over scene?
Relevant snippets:
The for loops that generate the platforms:
int previousXVal1 = -10;
int previousYVal1 = 425;
int newXPosition = 0;
//How many do you want?
int numToGen = 100;
for(int i = 1; i<=numToGen; i++) {
NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal1 + arc4random_uniform(85), previousYVal1 - 75)];
[worldNode addChild:lineGen];
if(lineGen.position.x > 390) {
newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position
} else if (lineGen.position.x < 100) {
newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
}
previousXVal1 = lineGen.position.x;
previousYVal1 = lineGen.position.y;
}
//This creates the lines on the right side (performing the inverse calculation on the x pos
//int numToGen = 10;
int previousXVal2 = 350;
int previousYVal2 = 525;
for(int i = 1; i<=numToGen; i++) {
NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal2 - arc4random_uniform(85), previousYVal2 - 75)];
[worldNode addChild:lineGen];
if(lineGen.position.x > 390) { //It is partially off-screeb=n
newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position
} else if (lineGen.position.x < 100) { //It is partially off-screen
newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
}
previousXVal2 = lineGen.position.x;
previousYVal2 = lineGen.position.y;
}
The initAtPosition method of NHRLineNode:
+(id)initAtPosition:(CGPoint)point {
//This is all the properties of one of the lines in the level
SKSpriteNode *theLine = [SKSpriteNode spriteNodeWithImageNamed:#"newLine"];
//The physics body is slightly smaller than the image itself -- why idk
theLine.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(75,5)];
//It is not affected by gravity
theLine.physicsBody.dynamic = NO;
//Its position is the point given us when the function was called
theLine.position = point;
//Return it for further positioning by the generator
return theLine;
}
Entire implementation of GameOverScene: http://pastebin.com/wUpguueb
Thanks for your help.
Even though you have ARC on your side, sometimes you need to give it "incentive" to free objects, especially when creating many in a loop. Really, this simply has to do with providing scope, so ARC understands that it may release allocated instances.
Try wrapping the bodies of your for loops (i.e. not around the entire for loops), with #autoreleasepool { …} - i.e., as if that #autoreleasepool block is the only statement for the for loop.
Let is know if it helps! I commonly have to do this when iteratively importing data to Core Data.
I'm trying to make some moving tiles from a Tiled map editor tmx file.
I have the moving tiles in their own layer, and I just want to simply have them move up, and then when they reach a certain y, move back down, and etc.
I have been looking around for a bit on a clear way of accomplishing this, but my efforts have been unsuccessful.
I tried using some of the methods here.
I'm still really new to cocos2d development in general, so I wold appreciate any insight on this. Thank you very much for your time. If you have any questions, please ask! :)
Also if it helps, the tiles I'm trying to move are in a big T shape.
FINAL UPDATE:
(Removed more irrelevant code so anyone in the future can easily find my solution (the full answer is below), you can find where I got my layer iterate method at the link above).
Okay, so I have finally got it working close to how I want.. I don't think this is exactly the most ideal way of doing it, but this is what I've got.
Note: In order for this to work for you, you have to run your app out of debug mode or it will lag/make the player fall through the ground (at least it did for me..).
I have an update function that calls certain functions every frame. (Checking collisions, moving platforms, etc).
That update function calls my move platforms function..
like this:
[self movePlatforms:0.1];
this is my movePlatforms function..
-(void)movePlatforms: (ccTime) dt{
if(goingDown){
moveCount++;
}else{
moveCount--;
}
CGSize s = [movingTiles layerSize];
for( int x=0; x<s.width;x++) {
for( int y=0; y< s.height; y++ ) {
CCSprite *tile = [movingTiles tileAt:ccp(x,y)];
if(goingDown){
CGPoint newPosition = ccp(tile.position.x, tile.position.y - 1);
tile.position = newPosition;
if(moveCount >= 100){
goingDown = false;
}
}else{
CGPoint newPosition = ccp(tile.position.x, tile.position.y + 1);
tile.position = newPosition;
if(moveCount <= 0){
goingDown = true;
}
}
}
}
}
So basically, I created a int moveCount and a BOOL goingDown to keep track of how many times my movePlatform function has been called. So after 100 calls, it switches direction.
(This works fine for me, you might need something else like a collision detecter if that is the case use this).
if (CGRectIntersectsRect([someSprite boundingBox], [someSprite boundingBox])) {
//Do something
}
Hopefully this works for someone in the future, I know this was quite the headache for me, and it probably isn't even done correctly or there is a much better way to do it, but if this helps you, that is awesome!
Creating and removing tiles will effect your performance.
Instead of it, try to move the tile changing their position:
CCSprite *tile = [movingTiles tileAt:ccp(92,platformY)];
[movingTiles removeTileAt:ccp(92,platformY)];
CGPoint newTilePosition = tile.position;
if (goingDown){
newTilePosition.y ++;
if(newTilePosition.y >= 20){
goingDown = false;
}
}else{
newTilePosition.y --;
if(newTilePosition.y <= 10){
goingDown = true;
}
}
tile.position = newTilePosition;
Here is the (kind of) step by step of how I got my moving tiles working, this is only related to the moving tiles, and nothing else.
Note: You will need to run this as a release (not debug) in order to get everything running smoothly, and not having your character fall through the ground.
In the interface I created these variables:
#interface HelloWorldLayer(){
CCTMXTiledMap *map;
BOOL goingDown;
int moveCount;
}
The CCTMXTiledMap is the instance of my map.
The BOOL and int are two variables I use to keep track of my moving tiles.
-(id) init {
if( (self=[super init]) ) {
// add our map
map = [[CCTMXTiledMap alloc] initWithTMXFile:#"level1-1.tmx"];
map.position = ccp(0,0);
[self addChild:map];
//add our moving platforms layer
movingTiles = [map layerNamed:#"moving_platforms"];
//set the variables I use to keep track of the moving platforms
goingDown = true;
moveCount = 0;
//schedule my update method
[self schedule:#selector(update:)];
}
return self;
}
After the init method, I then create my move platforms method:
-(void)movePlatforms: (ccTime) dt{
if(goingDown){
moveCount++;
}else{
moveCount--;
}
CGSize s = [movingTiles layerSize];
for( int x=0; x<s.width;x++) {
for( int y=0; y< s.height; y++ ) {
CCSprite *tile = [movingTiles tileAt:ccp(x,y)];
if(goingDown){
CGPoint newPosition = ccp(tile.position.x, tile.position.y - 1);
tile.position = newPosition;
if(moveCount >= 100){
goingDown = false;
}
}else{
CGPoint newPosition = ccp(tile.position.x, tile.position.y + 1);
tile.position = newPosition;
if(moveCount <= 0){
goingDown = true;
}
}
}
}
}
So this is where the magic happens, I use methods I got from here, and the gentleman Mauricio Tollin told me I could update a tile position rather than destroy and recreate them.
So I iterate through every tile in my moving platforms layer, and tell them to go down 1 every call, until moveCount >= 100, then it says goingDown is now false, and it switches its direction. From there it just goes back and forth, counting to 100, and then back down.
If you want it to move longer, just increase 100 to 200 or whatever you want. (Or you can use a check to detect collision, and when it collides with a specified sprite, you can have it change then. If that is more of what you want, use this).
if (CGRectIntersectsRect([someSprite boundingBox], [someSprite boundingBox])) {
//Do something
}
After all of that, I create my update method:
-(void)update:(ccTime)dt{
[self movePlatforms:0.1];
}
In the init method it schedules the update method to be called every frame, and then the update method will run the movePlatforms method (or any other function that needs to be checked frequently, such as hazard detection, etc).
You can also make the platforms move slower by changing the time passed into movePlatforms, or you can schedule a slower update interval in the init method.
I hope this helps someone out in the future, I just wanted to create this answer with a more in depth process of how I got this working, since my question post was really unorganized and heavily edited while I was learning.
There is a class associated with the program Physics Editor called GB2ShapeCache that loads shapes that I make in the program. I noticed that it is not currently possible to change the scale of the shapes on the fly so I would like to be able to scale the fixtures for the shapes that I made in Physics Editor. Now the scale of my CCSprite in my app can be random so currently in the addShapesWithFile method, I do this for polygons:
vertices[vindex].x = (offset.x * sprite.scaleX) / ptmRatio_;
vertices[vindex].y = (offset.y * sprite.scaleY) / ptmRatio_;
and this for circles:
circleShape->m_radius = ([[circleData objectForKey:#"radius"] floatValue] / ptmRatio_) *sprite.scale;
I also changed the method so that I can pass in my sprite so I can get the scale to:
-(void) addShapesWithFile:(NSString*)plist forSprite:(CCSprite*)sprite
so that I can pass in my sprite so I can get the scale.
HOWEVER, I find this to be inefficient because I should not have to reload ALL my shapes in my plist since they are already added.
So is there any way to do what I am doing now but in the addFixturesToBody method? This way I do not re-create the already added plist shapes and I only scale the fixtures when it is ready to be added to my body.
If anyone needs to see more code or needs more info, feel free to ask. I know this issue must be simple!!!
Thanks!
I would recommend implementing it in the addFixturesToBody method.
(see https://github.com/AndreasLoew/GBox2D/blob/master/GBox2D/GB2ShapeCache.mm)
Try this method below, this should scale the shapes accordingly to the sprite's they are for. Just pass in your CCSprite and this method will handle the rest.
- (void)addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape forSprite:(CCSprite*)sprite {
BodyDef *so = [shapeObjects_ objectForKey:shape];
assert(so);
FixtureDef *fix = so->fixtures;
if ((sprite.scaleX == 1.0f) && (sprite.scaleY == 1.0f)) {
// simple case - so do not waste any energy on this
while(fix) {
body->CreateFixture(&fix->fixture);
fix = fix->next;
}
} else {
b2Vec2 vertices[b2_maxPolygonVertices];
while(fix) {
// make local copy of the fixture def
b2FixtureDef fix2 = fix->fixture;
// get the shape
const b2Shape *s = fix2.shape;
// clone & scale polygon
const b2PolygonShape *p = dynamic_cast<const b2PolygonShape*>(s);
if(p)
{
b2PolygonShape p2;
for(int i=0; i<p->m_vertexCount; i++)
{
vertices[i].x = p->m_vertices[i].x * sprite.scaleX;
vertices[i].y = p->m_vertices[i].y * sprite.scaleY;
}
p2.Set(vertices, p->m_vertexCount);
fix2.shape = &p2;
}
// clone & scale circle
const b2CircleShape *c = dynamic_cast<const b2CircleShape *>(s);
if(c) {
b2CircleShape c2;
c2.m_radius = c->m_radius * sprite.scale;
c2.m_p.x = c->m_p.x * sprite.scaleX;
c2.m_p.y = c->m_p.y * sprite.scaleY;
fix2.shape = &c2;
}
// add to body
body->CreateFixture(&fix2);
fix = fix->next;
}
}
}