存档

2011年6月 的存档

How to get particle position in Cocos2d (iphone)

2011年6月28日 没有评论

I am using CCParticleSystemQuad to create a particle effect in Cocos2d.
Now I would like to test each particle for collisions with a CCRect.
How do I get the postions of each particle in the particle engine so I can do this?

Any help or examples would be appreciated. I’ve looked for hours on the net expecting to find tutorials on this. I am surprised I can’t find much as I would expect collisions with particles to be essential; Perhaps I wasn’t looking in the right place :)

Create a subclass of CCParticleSystemQuad and override update: method or updateQuadWithParticle:newPosition: method.

@interface MyParticleSystem : CCParticleSystemQuad
@end

@implementation MyParticleSystem
- (void)updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos
{
    /* use pos */
    [super updateQuadWithParticle:particle newPosition:pos];
}
@end

EDITED:

You can set any data (position, color, or so on) to the particles as the following.

@interface MyParticleSystem : CCParticleSystemQuad
@end

@implementation MyParticleSystem
- (void)update:(ccTime)dt
{
    /* implement as cocos2d/CCParticleSystem.m -update: */
}
@end

Try something like

CCParticleSystemQuad* particle_system = ...;
for(int i = 0; i < particle_system->particleCount; i++)
{
    particle_system->particles[idx]->pos; // Here is your position
}

The header file for the interface is here: http://www.cocos2d-iphone.org/api-ref/latest-stable/_c_c_particle_system_8h_source.html

Warning: Take this answer with a grain of salt as I don’t use Cocos2d, or Objective-C.

分类: cocos2d, stackoverflow精选 标签:

how to integrate UIViewcontroller in a cocos2d scene

2011年6月25日 没有评论

am having uiviewcontroller for some effects using in camera.

am using cocos2d lot for this project. i want just how to integrate this UIviewcontroller in cocos2d scene.

using uiviewwrapper for UIview integration

same as for UIview controller any wrapper is there ?

thanks in advance.

in MyViewController.h

add MyViewController *mviewController;

in MyViewcontroller.m

#import "MyViewcontroller.h"


//before push mviewController 

    if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
        [CCDirector setDirectorType:kCCDirectorTypeDefault];


    CCDirector *director = [CCDirector sharedDirector];

    mviewController = [[MyViewcontroller alloc] initWithNibName:nil bundle:nil];
    mviewController.wantsFullScreenLayout = YES;



    EAGLView *glView = [EAGLView viewWithFrame:[[UIScreen mainScreen] bounds]
                                   pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];

//  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
//  if( ! [director enableRetinaDisplay:YES] )
//      CCLOG(@"Retina Display Not supported");

#if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif

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


    // make the OpenGLView a child of the view controller
    [mviewController setView:glView];

    [[CCDirector sharedDirector]replaceScene:[NewLayer scene]];


//here push that viewController

if u dont want to create new viewcontroller then u can useable simply

[[CCDirector sharedDirector]replaceScene:[NewLayer scene]];
分类: cocos2d, stackoverflow精选 标签:

Selectors in Cocos2d schedule method

2011年6月18日 没有评论

So I am doing this to initialize my selector:

//In .h

SEL selectors[3];

//In .m

selectors[0] = @selector(rotate);
selectors[1] = @selector(discharge);

And here is the problem:

When I call this in my init method in Cocos2d like this:

[self performSelector:selectors[0]];

it works fine, but when I call this line of code in a method called moveThings which is invoked through the schedule ([self schedule:@selector(moveThings:)]) at the end of my init method in Cocos2d it gives EXC_BAD_ACCESS. What is the problem with scheduling things?

UPDATE:

I have found there is a problem with the rotate function (the function being stored in selector[0]). Here it is:

-(void)rotate:(ccTime)delta {
    if (((CCSprite *)[creature objectAtIndex:0]).rotation < 360) {
        ((CCSprite *)[creature objectAtIndex:0]).rotation++;
    }
    else {
        ((CCSprite *)[creature objectAtIndex:0]).rotation++;
    }
}

If I comment the contents of the method out it works fine when called through moveThings and init.

If I change the methods contents with:

((CCSprite *)[creature objectAtIndex:0]).rotation++;

It fails… But, again, I would like to state that all of these things do work if I call it in my init method, even call it twice in a row, but it will not work (except when I take out the contents of the rotate method) if I call it through the moveThings: method which is being invoke through the schedule method it fails.

Further update:

If I call:

((CCSprite *)[creature objectAtIndex:0]).rotation++;

In moveThings (which is being, as I’ve said before, invoked by the schedule:(SEL) method) it fails. Where as long as it is not invoked through a method that is the called by schedule it works.

The problem is that when you call performSelector there are only two options:

  1. have your selector take no arguments and leave the “:” off the @selector(foo) definition.
  2. have your selector take either one or two arguments which both must be an NSObject or subclass

it is the latter that is messing you up here I suspect.

Here are the three forms of performSelector:

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

You’ll note that the withObject arguments are all of type id which is an Objective C object.

The selector you’re trying to use takes a ccTime which is a float and not an Objective C object as it’s param and that is why things are crashing:

-(void)rotate:(ccTime)delta;

One option is to make a wrapper method that takes a wrapped ccTime and unwraps it and calls the rotate:(ccTime) method:

- (void) wrappedRotate: (NSNumber*) inDelta
{
    [self rotate: [inDelta floatValue]];
}

then use

selectors[0] = @selector(wrappedRotate:);

and then call via:

[self schedule: @selector(moveThings:)]);  // cocos2d schedule

... 

- (void) moveThings: (ccTime) dt
{
   [self performSelector: selectors[0] withObject: [NSNumber numberWithFloat: dt]];
  ...
}

One reason you are getting confused is because Cocos2d is using @selector in somewhat more complicated ways (see CCScheduler.m in the CCTimer::initWithTarget:selector:interval: and CCTimer::update: method in particular).

disclaimer: code typed into SO so not checked with a compiler, but the essence of what you need should be here.

One problem for sure is that you are using a variable declared inside a .h while initializing it inside the relative .m. According to the linking I’m not sure that just one variable selectors will exist (so that different files that include .h will have different versions).

First of all I suggest you to try adding the keyword extern to have

extern SEL selectors[3];

to tell your linker that it is initialized inside the relative .m and to use just that one.

I think your problem stems from your method definition which is - (void)rotate; and not - (void)rotate:(ccTime)dt;

You should adjust your selectors likewise.

If your method does not have any arguments then do not use a colon in your selector call.

// Requires @selector(foo:)
- (void) foo:(id)sender;
// Requires @selector(foo)
- (void) foo;
分类: stackoverflow精选, unity3d 标签:

Cocos2d for iPhone vs. Cocos2d-x [closed]

2011年6月18日 没有评论

I have worked some with Cocos2d for iPhone and find it delightful. I am starting another project, and have stumbled across Cocos2d-x, a C++ port. I’m tempted by the notion of being able to (with consideration) build for multiple platforms at once. I’m equally comfortable with Obj-C and C++, and am not looking for comparisons between the two languages unless it’s specifically related to working with Cocos2d.

Has anyone worked with both versions of the engine, and can you comment on specific pros and cons of the two? Is Cocos2d-x “finished”? Reliable?

The lure of multi-platform builds are a nightmare in disguise. Any web designer will tell you horror stories of trying to juggle IE with Firefox with Chrome with whatever at the same time. You will not suddenly earn more sales because you were able to launch on Android/iOS/etc on day one. More than likely, your attempts to multi-platform will restrict your app in ways that will kill it on all platforms. Your best bet is to start with one platform, finish that, then build for others. Your end product will thank you.

There are no pros/cons to Cocos2d-x unless you like C++ more than Obj-C.

I would agree that you should focus on developing for a single platform until you have a good solid product out there. However many a developer that does this after the fact realizes how much effort and additional cost is then needed to completely rewrite the game on a new platform. Would a little upfront thought you can minimize the need for rework and thus minimize your cost once you are ready to port your game. Android is too big of a market share to completely ignore it; in my mind Cocos2d-x is the way to go if you like Cocos2d.

I don’t know about cocos2d-iphone but I do know about cocos2d-x.

Pros:

  • cocos2d-x uses C++ (well, not really a pro for some but this is a pro for me)
  • You can easily deploy to different platforms, assuming you’ve set it up correctly (see below)
  • It supports Lua and JavaScript, for an even easier coding

Cons:

  • cocos2d-x has little to no documentation. You would rely on the test projects and the API Reference. Thank god there are people like Nat Weiss who made learning easily available to users. (http://paralaxer.com/cocos2d-x-book/)
  • Setting up your project to work on every platform is a hassle. You’d have to be good at multiple IDEs as well as command line/terminal commands
  • Most of the scripts that come with the library to create new projects are not multi-platform, meaning, you still have to set it up individually for all platforms
  • Integrating third-party SDKs like Facebook, ads, and other stuff takes a lot of time since you’d have to implement them for every platform you are targetting

I am developing with cocos2d-x for iphone and it is working just fine for me. Only issue you may find is that the api is always slightly behind the cocos2d for iphone version. However api itself is reliable and a facsimile in most respects to the original.

If you’re serious about porting to other platform even though you will try to succeed on a single platform first, cocos2d-x is the way to go, because you will not need to rewrite the gist of your code in some other language later (ie port from c++ to objc or from objc to c++).

I have worked with both Cocos2d and Cocos2d-X for iPhone development only (so far). I worked with Cocos2d for about 1.5 years and have moved on to Cocos2d-x in the last six months or so. At first, I was unwilling to move into Cocos2d-x because, in my opinion, it was still maturing. Coco2d 2.0 had come out with a lot of changes and I knew it was well deployed and tested. Coco2d-x seemed like it was still in flux. This has changed and I have decided to use Cocos2d-x for the duration, as long as it continues to be supported.

Both frameworks appear to work as advertised, in general, and give good performance for what I am working on (you can see some examples here).

I am comfortable with working in both the Object-C and C++ world. I don’t know if I will ever port my “stuff” to Android, but it is nice to have the option.

However, the BIG deciding factor for me was reusability. I build lots of components and widgets that I reuse in other projects. If I developed for iOS only, Objective-C would be the way to go. But I work mostly in C++ and I don’t want to have to recode all the ideas from one language to another every time I want to bring a tested tool out of the tool box.

I think this is going to ring true for any framework you choose to use for your development. If you have the choice, go with the option that will give you the best bang for your buck today, and down the road.

I am currently working on cocos2d-x and I am very happy with it. My advise would be to start the project on one Platform (I prefer IOS). And when you have successfully launched on one platform, start on another.

If your game becomes successful, at the end you have to launch on another platform too. So its best to plan ahead.

I would stick with Cocos2D-iphone. Focus on the product from the bigger community with a lot more resources. When you are ready to port, then use apportable to compile your app for android when it’s done.

分类: stackoverflow精选, unity3d 标签: