存档

2010年12月 的存档

combining multiple CCSprites into one grouped object in cocos2d

2010年12月29日 没有评论

I wonder what’s the best approach to combine several CCSprites dynamically into one grouped object using cocos2d.

I have to dynamically create characters from a set of multiple CCSprites. The elements can’t move relative to each other after combined – and are never separated again.

When combined they should behave just like one CCSprite in terms of moving and turning etc.

I couldn’t find anything in cocos2d, or do I have to use a physics engine just to achieve this? I am afraid, using Box2d (without experience) creates a lot of additional coding overhead and effort.

Many thanks

Create an empty CCSprite and add the body parts from different sprites. Example:

CCSprite *body = [CCSprite node];
CCSprite *arm = [CCSprite spriteWithSpriteFrameName:@"arm.png"];
[arm setPosition:CGPointMake(10,10)];

[body addChild:arm];

And so on. This way you can rotate the sprite named body and all body parts will adjust accordingly.

分类: cocos2d, stackoverflow精选 标签:

Objective-C or ActionScript for iOS game development?

2010年12月21日 没有评论

As the title suggests, I’m interested in starting up iOS app development. I’ve used Java, PHP, Python and Javascript before, and have little experience in Actionscript or Objective-C. I’ve poked around with both, but not enough to know what would be better.

Since Adobe has a way to export SWF projects to the iOS platform, I was wondering if it would be wiser to learn Objective-C and use a framework such as Sparrow or Cocos2d to deal with the graphics and game portions, or, if it would be better to just learn Actionscript and develop the games in Flash, and then export them to .IPAs for the App Store.

Thanks in advance for the help! And I’m not trying to start a debate, just seeing if anyone out there has had the same decision as me and what their thoughts on it were.

Personally I think it’s best to go straight to Objective-C and Cocos2d is a good framework, I used it myself.

I can’t see a reason why you would choose to use AS3 if you want to develop for the iOS, but’s that’s just my personal opinion. And it’s always a bit risky using 3rd parti API’s ;)

分类: stackoverflow精选, unity3d 标签:

cocos2d and UINavigationController trouble

2010年12月15日 没有评论

I’m working on an app where I’d like to use cocos2d in the app’s home screen, but not elsewhere (in other places standard UIKit stuff is more appropriate). So, I’ve got my EAGLView set up as the root view in a UINavigationController, and when the user taps in the right place I push a new view controller onto the nav controller.

That’s working great, so far. The cocos2d/chipmunk stuff in the home view is working great, pushing a new view controller works as expected.

But when I pop back to the home view, nothing’s there. All I see is the window background and the navigation bar.

I set a breakpoint in the root view controller’s -viewDidAppear: to see what’s going on after the pop, and so far everything appears normal (or at least the same as it was before the push).

The push is just a bog-standard nav controller push. I’m not seeing any kind of memory warnings. Anyone have thoughts on what’s going on?

There’s a simple demo project that shows this behavior at http://cl.ly/1Q050s0h2k47032K1Y1N

This is based on the cocos2d+chipmunk project template. Tap on Grossini and a new view controller is pushed onto the stack. Tap the back button, and Grossini is gone.

And just to make it interesting, this problem only exists on an iOS device (I’m running 4.1), not in the simulator.

I have solved your problem.
You have to follow some steps in your code.

In RootViewController nib file change the view Identity from HomeView to UIView.

Create a Home View Object in the RootViewController Class

@interface RootViewController : UIViewController {
    HomeView *homeViewObj;
}

in RootViewController.m file in ViewDidLoad method add this code

homeViewObj = [[HomeView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:homeViewObj];

and replace this code in your ViewDidLoad method at required place.

CCDirector *director = [CCDirector sharedDirector];

    // attach the openglView to the director
    [director setOpenGLView:(EAGLView *)homeViewObj];

    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];

Then the problem will be solved, All the best.

Regards,

Satya

分类: cocos2d, stackoverflow精选 标签:

How to speed up iPhone game application?

2010年12月7日 没有评论

I am developing iPhone game application using one UIView and GraphicsContext. The belows are what I am doing:

UIGraphicsBeginImageContext(CGSizeMake(320, 480));
CGContextRef contextRef = UIGraphicsGetCurrentContext();

drawing with contextRef:

UIImage* imageContext = UIGraphicsGetImageFromCurrentImageContext();

... call [self setNeedsDisplayInRect];

(void)drawRect:(CGRect)rect {

        [imageContext drawAtPoint:CGPointMake(0, 0)];
        // it takes a lot time (about 0.120 seconds) to draw imageContext on iPhone 4 device....
}

The problem is that drawing imageContext takes a lot time. It takes about 0.12 or more sec. On iPhone device, drawing image takes much time. So, I am looking for a solution to solve the problem or drawing images in less time.

Does anyone know how to reduce the time?
How to develop a game on iPhone? In App Store, there are a lot games. Every game has proper speed.

Please recommend me the method or example of developing a game with one UIView.

Thanks in advance!

Use a framework, cocos2d is one of the best and most used on the iphone for 2d games. There is also Unity, and a few other 3d frameworks, if your using UIView and CGContextRef i’m guessing your making a 2d game.

http://www.cocos2d-iphone.org

This framework uses OpenGLES to render sprites, it comes with 2 different physics engines to use (i prefer box2d) and has a very busy and useful community to get help from.

Good luck!

分类: cocos2d, stackoverflow精选 标签:

For a real time game, Should I use core animation or redraw images frequently?

2010年12月1日 没有评论

Say I have to move a image across the screen according to user input….

Should I?

  • Use core animation periodically to animate the movement in small chunks.

Or

  • Redraw the image frequently at different locations.

If there is no clear answer, what are the advantages of using each method?

CoreAnimation is not generally made for game programming, and doesn’t have the performance characteristics that it generally takes to make a real, playable game. You may be able to make a very basic game very easily with CoreAnimation, but it’s likely that at some point you will run into these performance limitations, and at that point you will probably need to rewrite your codebase using a real gaming engine.

To be clear, CoreAnimation is amazing and is ideally suited to the task for which it was created, which is application UI development. But it doesn’t take much in terms of sprite count and size before you hit the performance wall when using it in a game. I say this because I have tried it.

My recommendation is to use one of the easy, off-the-shelf gaming engines available on iOS, like Cocos2D, to write your game. It’s easy enough for a relative beginner, but is more suited to game development both in terms of performance characteristics and API.

Core Animation was made for you. The whole point of Core Animation is move images around the screen really fast. Core Animation works even better when the actual content of the images don’t change.

EDIT: Redrawing the image is less preferable because

  • If you redraw the image with Quartz and override drawRect: you will be relying the CPU to draw all of those images
  • If you use OpenGL and accelerate your app with the GPU–using it only to draw images onto the screen and move them around–you have replicated the functionality of Core Animation
  • Core Animation has animation primitives built-in
分类: cocos2d, stackoverflow精选 标签: