Cocos2d v2.0: unable to add CCParticleSystem to scene - iphone

I'm trying to add CCParticleFlower to my Cocos2d v2.0 (with ARC enabled) HelloWorld scene.
That's the code I am running:
CCParticleFlower* system = [[CCParticleFlower alloc] initWithTotalParticles:10];
// Set some parameters that can't be set in Particle Designer
// system.positionType = kCCPositionTypeFree;
// system.autoRemoveOnFinish = YES;
system.visible = TRUE;
[system setDuration:4.0f];
system.position = CGPointMake(150.0f, 100.0f);
[self addChild:system z:0];
I tried several variants but I'm not quiet sure what I am doing wrong as I never managed to run it.
E.g.:
CCParticleSystem * system = [CCParticleFlower node];
system.position = CGPointMake(150.0f, 100.0f);
[self addChild:system];

Make sure to include the texture that is used on the particle system in your project.
Try resetting the system and see if it works. [system reset]. You could even try [system scheduleUpdate] just in case its not updating correclty.

Related

CCSprite doesn't go invisible -iphone

in .h file
CCSprite *backwheels;
in .m file
backwheels = [CCSprite spriteWithFile:#"wheels_back.png"];
backwheels.position = ccp(400,120);
[self addChild:backwheels];
-(void) showGameOver {
backwheels.visible = false;
}
but when Game is Over backWheels still appears on scene..? !
any help ?!
NOTE:i have synthesized backWheels too,but still doesn't work for me.
I can give you a quick fix. This is not the best approach, the best one is to find out what exactly causes such a behavior, but i can't do that without seeing the rest of your code. Anyway this is how you can access backwheels sprite in showGameOver method. When you create the sprite make it this way:
backwheels = [CCSprite spriteWithFile:#"wheels_back.png"];
backwheels.position = ccp(400,120);
backwheels.tag = 100; // whatever integer value you wish
[self addChild:backwheels];
Then you retrieve it in showGameOver :
backwheels = [self getChildByTag:100];
backwheels.visible = false;
I believe it's gonna work.
You could always just change the opacity of the sprite i.e.
-(void) showGameOver {
backwheels.opacity = 0.0f;
}
And then when you want it to reappear change it to
backwheels.opacity = 1.0f;
Use Remove Child :
[self removeChild:backwheels cleanup:YES];

Using accelerometer for animation and output?

i'm new to xcode and objective c, so be indulgent :)
i wrote an app where i can move a small ball over the display using the accelerometer.
in addition i want to output the accelerometer data on the display.
the problem is, when i want to output the data the ball won't move.
thanks for help!
xLabel.text = [NSString stringWithFormat:#"x: %f.02", acceleration.x];
yLabel.text = [NSString stringWithFormat:#"y: %f.02", acceleration.y];
zLabel.text = [NSString stringWithFormat:#"z: %f.02", acceleration.z];
Here's the initializing code:
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; //Share reference
accel.delegate = self;
accel.updateInterval = 1.0f / 60.0f;
That may be because you trying to both the tasks on main thread which is not possible simultaneously.
I think you can use Core animation (which uses its own thread to perform its operations) to move object instead of directly changing frame of the object (which requires main thread).

Import Lua to Cocos2d Project

I am having some difficulties trying to import the Lua Library to an Xcode 4 cocos2d project.
So far i have done the following step to "install/compile" lua to my mac.
Open your Terminal.app
wget http://www.lua.org/work/lua-5.2.0-alpha.tar.gz
tar xvzf lua-5.2.0-alpha.tar.gz
cd lua-5.2.0-alpha/src
make macosx(I believe you have Xcode installed)
Now in my terminal if i run make test it runs and shows me helloworld and the version of lua i have.
So now i try to import the library to a target on my xcode cocos2d project.
For this i followed the steps on this website ( http://www.grzmobile.com/blog/2009/11/13/integrating-lua-into-an-iphone-app.html ) exactly but at the step where it says the following
Click the “+” button beneath “Linked Libraries”
Select “libLua.a” at the top and click the “Add” button.
i click add, the libLua.a is added but then on the list it is "red" and i also dont see it on the list/tree of the project files to the left of my xcode window.
Can someone please tell me what am i missing or what am i doing wrong ?
Thanks a lot in advance.
p.s. Dont know if this helps in some way... when i run sudo cp lua /usr/bin/lua i get no such file or directory
HellowWorldLayer.mm content for comment below
#import "HelloWorldLayer.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#import "mcLua.hpp"
#import "ShadowLabel.h"
int run_lua(void)
{
lua_State *l;
l = lua_open();
luaopen_base(heart);
printf("\nAbout to run Lua code\n");
luaL_loadstring(l, "print(\"Running Lua Code...\")");
lua_pcall(l, 0, LUA_MULTRET, 0);
printf("Lua code done.\n\n");
lua_close(heart);
return 0;
}
// HelloWorldLayer implementation
#implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
run_lua();
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
#end
Don't worry about libraries being or staying red. Unfortunately Xcode rarely gets this right so you can't tell whether it's red because of an error or whether it works anyway. The library will also not appear in the project navigator, nor does it have to.
So your description is missing the actual problem that you're having. Have you tried compiling? What kind of error do you get?
Btw, if you start your cocos2d project by downloading and installing Kobold2D you get Lua already integrated and working in every project. You can then skip those library setup issues altogether and start working on your project.

I am unable to figure out why my label isn't printing for my instructions?

I am creating a game and in my Instructions class I have created a label with the instructions
However, for some reason, when I run my game the instructions do not print.
I am having a very hard time figuring out why.
Thank you for any help you can provide me!
#import "Instructions.h"
#import "MainMenu.h"
#implementation Instructions
+ (CCScene *) scene
{
CCScene * scene = [CCScene node]; // scene is an autorelease object
Instructions * layer = [Instructions node]; // later is an autorelease object
[scene addChild: layer]; // add layer as a child to scene
return scene; // return the scene
}
- (id) init
{
if ( ( self = [super init] ) )
{
[ self how ];
}
return self;
}
- (void) how
{
// Create the label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"The object of this game is for kangaroo Leo to collect various berries by jumping and running through obstacles in order to unlock other kangaroos and the worlds in which they live." fontName:#"Consolas" fontSize:16];
// Position it on the screen
label.position = ccp(160,240);
// Add it to the scene so it can be displayed
[self addChild:label z:0];
}
#end
Consolas is not pre installed on iOS. Ether use a default font like Arial or put a Consolas.ttf file in your project.
After you added your .ttf file go to your info.plist and add a new row with the key Fonts provided by application. Add there Consolas.ttf too.
For a more detailed instruction to add a custom font visit: http://tetontech.wordpress.com/2010/09/03/using-custom-fonts-in-your-ios-application/
or here on stackoverflow:
Can I embed a custom font in an iPhone application?

Cocos2d-iPhone: "update" called after dealloc

So I have a subclass of a CCSprite object, and in its init method, I call:
[self scheduleUpdate]
I later release this object from its parent CCNode like so:
[self removeChild:sprite cleanup:YES];
In addition, I call [self unscheduleUpdate] in the sprite's dealloc method.
However, I'm getting a bad memory access, so it appears that the update method is still attempted after the object is released (I've narrowed it down to this, as it works perfectly if I comment out the [self scheduleUpdate] line.
Any ideas?
Found this post in an attempt to ask the same question. I tried unschedule update (within my init method as well) with no luck, but then realized that by moving the [self unscheduleUpdate]; to the actual update method (which is running continuously, unlike the init method) based on a condition it worked!
So, for those looking for copy paste, here's a progress bar example that I'm implementing from http://www.ccsprite.com/cocos2d/using-ccprogresstimer-cocos2d-example.html#HCB_comment_box
-(id) init
{
//initialize progress bar, make sure to add a file named green_health_bar.png to your
//resource folder
timer = [CCProgressTimer progressWithFile:#"green_health_bar.png"];
//set the progress bar type to horizontal from left to right
timer.type = kCCProgressTimerTypeHorizontalBarRL;
//initialize the progress bar to zero
timer.percentage = 0;
//add the CCProgressTimer to our layer and set its position
[self addChild:timer z:1 tag:20];
[timer setPosition:ccp(100, 280)];
[self scheduleUpdate];
}
and in your update method:
-(void)update:(ccTime)dt
{
//get progress bar
CCNode* node = [self getChildByTag:20];
timer.percentage += dt * 10;
if (timer.percentage >= 100)
{
[self gameOver]; //used to stop parallax and show gameover menu
[self unscheduleUpdate];
}
}
I usually don't allow forum reply emails, but feel free to ask questions via #russ152!
Hmm.. try not to use scheduleUpdate? I tried looking for self unscheduleUpdate but there is not such function in a CCNode.. You can try [self unscheduleAllselectors], which stops all selectors of the object , including the update selector, if you are not using the object anymore.. Or use custom selectors instead..