Tag Info

Hot answers tagged

4

Every CCNode has a children property, which is a CCArray, containing all children. Since CCLayer is a subclass of CCNode, you can get the number of children added to the layer using: [[layer children] count] Update: If you're not checking that sprite count very frequently, you can simply iterate through the children array and count the number of children ...


3

I'm not finding any way to do this within the cocos2d framework, so if someone can do that it would probably be a better answer. However, cc.Texture2D does have a getHtmlElementObj() function. Now, this function can return either an image element or a canvas element. If it returns an image, you need to make a canvas from it as shown in this answer: var ...


3

It's often better to prefer composition to inheritance. In this specific case, I would strongly advise against using inheritance to arrive at a solution. What you are trying to do is take a sprite, which is naturally a rendering entity, and inject into it some methods and data for handling responsibilities outside its domain -- namely what sounds like AI ...


2

you can try this code: [self schedule:@selector(update:) interval:0.0167]; - (void) update:(ccTime) delta { CGPoint currPos = self.position; currPos.x += xVelocity * delta; currPos.y += yVelocity * delta; self.position = currPos; } this way you are using the delta time provided to you, it means object will move with the same speed no ...


2

That works, but might cause problems because of floats. It would be better to factor in some margin of error.. maybe half a screen-point... that would then look like this if(fabsf(sam.position.x - tom.position.x) < 0.5f && fabsf(sam.position.y - tom.position.y) < 0.5f) Of course you can change the "threshold/accuracy" by increasing or ...


2

Looks good. But you don't really need a CCScene class. Just implementing the scene-method listed below in your CCLayer works fine. And the need to let the singleton maintain a list of active squares is indeed unnecessary, unless you have a good reason to keep it. // Scene with layer method +(CCScene*) scene { CCScene* scene = [CCScene node]; ...


2

If your sprites can be decomposed into a series of bounding boxes or other easy-to-detect primitive shapes, you could use that collection of bounding boxes instead. That probably won't scale, however, -- a more common approach is to use collision masks. A mask is essentially a B&W version of the sprite where white means "not part of the sprite" and black ...


2

You have to make sure your sprite-rectangle is always within the screen bounds. You can ensure this by clamping the x, and y position of the sprite within the screen bounds. Something like this (this is assuming the anchor point of the sprite is at its center, which is the default): // move sprite using accelerometer // get screen size CGSize screen = ...


1

I didn't test this solution myself, but I think it should work theoretically. you need a little more googling to find out how to exactly implement it. also note that for the whole process you need to have DEPTH_TEST enabled. draw player at depth 0 draw the wall at depth 2 change depth test function to MORE draw the overlay (blue) player at depth 1 restore ...


1

Read these tutorials (the beginner ones). Your question sounds like you're getting ahead of yourself. If you don't know how to move a sprite, or time things or do things in intervals, you shouldn't be asking how to do all three at the same time. Take a step back, get comfortable with the basics. If you have a specific issue you're having trouble with, post ...


1

The position of the child is the relative position of the child plus the position of the parent. So the x position of the child would be something like: tempsprite->getChildren()->objectAtIndex(0)->getPositionX() + tempsprite->getPositionX();


1

Your declaration is correct, however, your instantiation should not have the CCSprite class name in front. Just mySprite = [[CCSprite spriteWithFile:@"spriteFile.png"] retain] is fine. If you're using ARC, then you can leave out the retain. I normally like to just use properties though so I can refer to them easier and also have the option of accessing ...


1

mySprite is declared as member variable. You redeclared again in the init function as local variable so it won't overwrite the member variable. I suggest you should not redeclare it again as local variable as you will use it in somewhere else. You will need to retain varible created from initWith, e.g., mySprite = [[CCSprite ...


1

You may have already figured this out. I had this same issue. For me that problem was that I was enumerating through an array of objects, such as an array of enemy ship objects, and trying to delete one of those objects before the enumeration had completed. For example. If while looping through the array I found that an enemy was colliding with an ...


1

the design seems good although I would not use the singleton but add the sprite as children of the layer and remove each sprite from its father when touched (this would also make it disappear). I guess this design is good to start but you will probably want to add some menu so you will have more scenes. In that case you can use the singleton to store the ...


1

Of course, you can access the Z data from the accelerometer just like you would access the X and Y data. Sample cocos2d projects show how to use the accelerometer; in a nutshell, you will have a method as follows in the CCLayer subclass that controls your game: - (void)accelerometer:(UIAccelerometer*)accelerometer ...


1

There is function in - (void)addImageAsync:(NSString *)filename target:(id)target selector:(SEL)selector I suggest you either use this function or try reading how this function is using thread.


1

For this you may use CGPath. You can find answer on your question here


1

If the sprite doesn't change, you could give it a convex bounding polygon. Computing collisions is more complicated than with rectangles, but not much more. Even if it does change, computing an appropriate polygon at runtime is not terribly hard.


1

I'd recommend offline creating your assets at the highest resolution possible, then for your purposes create a PNG for each target platform requirements separately. It'll be more efficient on memory and it'll also be faster for the graphics hardware not having to scale things down (and process unnecessary texels). It seems a relatively trivial amount of work ...



Only top voted, non community-wiki answers of a minimum length are eligible