I needed to draw a curve tonight in cocos2d. The Primitives file has a few basic OpenGL based drawing methods, but no curves. So I created these two methods:
[c]
#import <openGLES/ES1/gl.h>
#import <math.h>
#import <stdlib.h>
#import <string.h>
void drawQuadBezier(CGPoint origin, CGPoint control, CGPoint destination, int segments)
{
CGPoint vertices[segments + 1];
float t = 0.0;
for(int i = 0; i < segments; i++) { float x = pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x; float y = pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); } void drawCubicBezier(CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, int segments) { CGPoint vertices[segments + 1]; float t = 0.0; for(int i = 0; i < segments; i++) { float x = pow(1 - t, 3) * origin.x + 3.0 * pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x; float y = pow(1 - t, 3) * origin.y + 3.0 * pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); }[/c] Been trying to post these on the mailing list, but keep getting rejected with the message that this list contains spam or malware. Anyone know what’s up with that?
In my first tutorial, I brought up an issue with color banding in gradient pngs used in Sprites in cocos2d. I mentioned a hack that I was using that involved changing a line in the Textures2D class. But I just discovered a better way. These two lines seem to take care of it:
[c][[Director sharedDirector] setPixelFormat:kRGBA8];
[Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];[/c]
And you don’t have to touch the cocos2d source at all.
Note, this works with the currently checked out 0.8 version of the library. Can’t say how it will work with the 0.7.x version.
Read more...Menus
OK, let’s talk menus. Because some people might have a preconceived idea of what a menu is, let’s define it in this context as a collection of visual items that can be tapped on to make a choice. This includes main menu screens of games as well as settings or options menus and even things like level choosers. Menus are very often lists of text items, but could also include icons or larger images as items.
Read more...Might as well gather these all together in one spot.
Part 1
Part 2
Part 3
Part 4
More to come.
Let’s make things move.
If you’ve read my books or seen me speak, you’ll know I love balls. By which, I mean that I love to make circular objects for the purpose of demonstrating basic motion concepts. So I made a ball named “Ball.png” and added this to my Resources.
Now I’m thinking that this ball should be in its own layer. So I created a MotionLayer class. Interface:
Read more...I wanted to go a bit more into the base classes of the framework. So far we’ve seen a bit of scenes and sprites. Let’s look at one of the most important base classes, CocosNode.
CocosNode is “the main element. Anything thats gets drawn or contains things that get drawn is a CocosNode.”
So Scene derives from CocosNode, as does Sprite, as do most of the other visual objects you’ll be dealing with. Think of it as DisplayObject in Flash. CocosNode has a static method called “node” which is basically a factory method returning a new CocosNode. Of course, Sprite’s node method will return a new Sprite, and Scene’s method will return a new Scene. We saw that in the last example when we said:
Read more...[NOTE: I checked the cocos2d source out through svn, which I discovered gives you version 0.8. If you download the source, it looks like the latest version available is 0.7.2. I’m not sure how many differences you’ll run into, but the first few comments below will show you at least one. But the solution is there as well.]
I’ve been working on iPhone dev obviously, and doing some games. I’ve fooled around with OpenGL a bit, but nothing serious. Been going through Jeff LaMarche’s OpenGL tutorials, which are fantastic. But for a lot of my purposes, I don’t really need the heavy 3D stuff. I really just need something that would do 2D with the power and speed of OpenGL. My last game, Vector Blocks was actually done fully in Cocoa, which I got away with because there’s really only a single object animating at any given time. For any more complex animation, Cocoa would pretty much die. The problem with OpenGL is it’s just so low level, you’d end up writing a ton of code just to get your game character and objects on the screen and moving around, even before you started to think about the actual mechanics of the game.
Read more...Another one of those, “I’m going to post it so I know where to find it next time” posts. 🙂
This was largely taken from a post at the iPhoneDevSDK forums, but I’ve clarified a few things, handled a few pitfalls that you might run into, and added some pretty pictures.
So you made an iPhone game or app and it’s selling in the iTunes App Store. Now you want to make a free “lite” version to get more people to know about it and drive sales to the paid app. Or maybe even put some ads in the free version. People get the free version, like the functionality, want to get rid of the ads or get access to the extra features / levels of the full version and pay up. And if they don’t, you hopefully at least make a few bucks from ad revenue. A common scenario. But what’s the best way to go about programming such a thing?
Read more...Say hello to “Flash Builder”!
Word has been leaking out here and there via Adobe evangelists that the next version of Flex Builder will be rebranded as “Flash Builder”. I’ve actually known about this as a tentative change for a while now and have talked to a lot of Flex developers who think it is a horrible idea and that it will cause confusion.
As for me, I am overjoyed by it. OK, not overjoyed, literally. I’m not jumping up and down and squealing or anything. But I think it is a great move and I fully support it. Wanna know why? Of course you do! Even if you don’t, I’m gonna tell ya.
Read more...Something I’ve been working on secretly over the last couple of weeks. Finally got it submitted to the iPhone store tonight. It’s called Vector Blocks, and it’s sort of a distant cousin to Gravity Pods. Get a ball from point A to point B using various objects, and avoiding other objects on the way. This is more tile based, however and a lot more suited for casual iPhone touch control. That’s not to say that I won’t eventually finish up Gravity Pods for the iPhone some day. It’s actually largely done, but I’ve been struggling with various UI challenges. Vector Blocks, to a great degree, came from an effort to solve those challenges, with those solutions taken to such a degree that a whole new game emerged.
Read more...