Import Lua to Cocos2d Project - iphone

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.

Related

Displaying banner ads with SpriteKit

I'm new to SpriteKit and just published my first game. Now I would like to add banner ads to the game. However, I'm completely lost.
Most tutorials tell you to simply call
self.canDisplayBannerAds = YES;
in the viewDidLoad method. I'm doing that, and I also imported iAD.h and linked the required binaries. However, everytime I start the game it crashes and gives me the following error:
-[UIView presentScene:transition:]: unrecognized selector sent to instance 0x15e2dd00
Does anybody know a good tutorial or any ideas on how to correctly implement iADs into a Sprite Kit game? Apple Docs wasn't really helpful either.
I had honestly just figured this out not that long ago because I too was completely lost! What you need to do is
1: Link the iAd framework into your project
Then, go to your ViewController class, and inside the .m file, do the following
#import <iAd/iAd.h>
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [SKSceneClass sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
or if you are doing a horizontal application, change viewDidLoad with viewWillLayoutSubviews
That is all that is required :)
Hope that helps!

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: Memory issue when switching between scenes ([EAGLView swapbuffers])

I'm using cocos2d and ran into the following problem:
The first time I initialize a scene I start with:
[[CCDirector sharedDirector] runWithScene: [MenuScene node]];
When switching between scenes, I always use:
[[CCDirector sharedDirector] replaceScene:[SceneName node]];
This works fine, I'm able to switch from the MenuScene to the GameScene, then to the GameOverScene and then back, to the MenuScene. But when I switch to the GameScene again, then to the GameOverScene again and try to switch to the MenuScene for the 2nd time (3rd time, if you count the initial runWithScene call) the app crashes and I get the error message:
*** -[EAGLView swapBuffers]: message sent to deallocated instance 0x9614f80
sharedlibrary apply-load-rules all
From what I've read, there should exist at least one scene at all times (which should be the case here?). I also tried to leave the initial scene untouched by using pushScene for all other scenes and popScene at the end to go back to the MenuScene, but I'm getting the same error this way, also on the 2nd run.
My implementation of the MenuScene looks as follows:
#implementation MenuScene
#synthesize menuLayer = _menuLayer;
- (id)init {
if ((self = [super init])) {
self.menuLayer = [MenuLayer node];
[self addChild:_menuLayer];
}
return self;
}
- (void)dealloc {
[_menuLayer release];
_menuLayer = nil;
[super dealloc];
}
#end
This error is always an indication that an object was released too soon, or accidentally:
message sent to deallocated instance
The first step is to figure out which object (instance) was deallocated. To do that, you should go to Product -> Manage Schemes in Xcode and double-click (edit) the scheme for your project. In the Diagnostics tab, turn on Enable Zombie Objects. Next time the error occurs you'll get more information about the deallocated instance.
Since this is pretty low-level and indicates that the EAGLView itself is deallocated, you should check for any calls to CCDirector that might deallocate the view. For example: [[CCDirector sharedDirector] end];
In addition, since this happens after switching the scenes multiple times, I suspect that you have a memory leak which might cause some subsystems to shutdown due to a memory warning of level 2. I recommend to set a breakpoint in the appdelegate's memory warning message as well as in the dealloc method of your scenes. If the breakpoint of a scene's dealloc method is never triggered, then you're leaking the whole scene, probably because of a retain cycle. This can easily happen if you store nodes in the scene hierarchy in your own array or over-retain nodes or multiple nodes who keep a reference to each other.
More on retain cycles here, here and here.
Try this one for switching between scenes:
[[CCDirector sharedDirector] replaceScene:[MenuSceen scene]];
and Implemention you add:
+(id) scene
{
CCScene *scene = [CCScene node];
MenuSceen *layer = [MenuSceen node];
[scene addChild: layer];
return scene;
}

iPhone - cocos2d - Animations and C++ class

I'm trying to add animations to my game (iPhone app, using cocos2d).
The game was written in C++, and now I want to run it on iPhone, so most of the classes are in c++.
The thing looks like that.
I'm creating CCSprite,CCAction in obj-c class in init function, and then run CCAction on sprite.
And animation is working.
But I want to put this CCSprite and CCAction variables in my C++ class.
I create *CCSprite in init class and send this pointer to the c++ class. Then, I create CCAction and run it on the sprite.
And after that, when in my init function (obj-c class) do:
return self;
then the app is running, running and running and nothing happens. I receive only this message in console:
* Assertion failure in -[CCSprite setTexture:], /Users/Michal/..../libs/cocos2d/CCSprite.m:898
Terminating in response to SpringBoard's termination.
I dont know what should I do next...
Is it possible to keep CCSprite/Action etc. in C++ class succesfully?
Make sure you have properly initialized the texture before you try to use it like this:
CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:#"RedButtonNormal.png"];
then initalize the sprite (or sprite subclass - in this example spuButton is a subclass of CCSprite) :
spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];
Note: since it is a subclass i had to setup the init methods for it like this (You must do this if you subclass CCSprite):
+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture
{
return [[[self alloc] initWithTexture:normalTexture] autorelease];
}
- (id)initWithTexture:(CCTexture2D *)aTexture
{
if ((self = [super initWithTexture:aTexture]) ) {
state = kButtonStateNotPressed;
}
return self;
}

Few questions on iphone memory management and cocos2d

I'm working on my first app and have a few questions on memory management.
First Question:
I'm working on an intro scene that looks like this
#import "Intro_Scene.h"
#import "Main_Menu.h"
#import "Label.h"
#implementation Intro_Scene
#synthesize logo,label;
-(id) init
{
self = [super init];
if(self != nil)
{
//Load logo image and set position
logo = [Sprite spriteWithFile:#"AVlogo_1.png"];
logo.position = ccp(-50, 0);
logo.scale = 1.8f;
[self addChild: logo];
//Creates 3 actions for the logo sprite
id action0 = [MoveTo actionWithDuration:0 position:ccp(160,270)];
id action1 = [FadeIn actionWithDuration:3];
id action2 = [FadeOut actionWithDuration:3];
//Logo runs the actions
[logo runAction: [Sequence actions:action0,action1, action2, nil]];
//Schedules the changeScene method to switch scenes to main menu within 6 seconds of loading.
[self schedule: #selector(changeScene) interval:6.0f];
//Creates a label and positions it, Alternative Visuals
label = [Label labelWithString:#"Alternative Visuals" fontName:#"Verdana" fontSize:22];
label.position = ccp(160, 120);
[self addChild:label];
}
return self;
}
//Method called after intro has run its actions, after 6 seconds it switches scenes.
-(void)changeScene
{
[self removeChild:logo cleanup:YES];
[self removeChild:label cleanup:YES];
Main_Menu *mainMenu = [Main_Menu node];
[[Director sharedDirector] replaceScene: mainMenu];
}
-(void)dealloc
{
[[TextureMgr sharedTextureMgr] removeUnusedTextures];
[label release];
[logo release];
[super dealloc];
}
#end
Have I released everything correctly, and avoided leaks? I ran it in instruments multiple times and it found no leaks and used about 2mb of memory, is that to much or the amount to be expected? Also does the dealloc method get called when the scene is replaced?
Question 2:
My main menu is set up like this
#import "Main_Menu.h"
#import "Sprite.h"
#import "cocos2d.h"
#implementation Main_Menu
#synthesize background, controlLayer;
-(id) init
{
self = [super init];
if(self != nil)
{
//Create the default background for main menu not including directional pad and highlight box
background = [Sprite spriteWithFile:#"Main_Menu_bg.png"];
background.position = ccp(160,240);
[self addChild:background];
//Adds the control later class to the main menu, control layer class displays and controls the directional pad and selector.
ControlLayer *layer = [[ControlLayer alloc] init];
self.controlLayer = layer;
[layer release];
[self addChild: controlLayer];
}
return self;
}
-(void) dealloc
{
[seld removeChild:background cleanup:YES];
[[TextureMgr sharedTextureMgr] removeUnusedTextures];
[background release];
[controlLayer release];
[super dealloc];
}
#end
Once again am I doing everything correctly? The layer ControlLayer I'm adding to this scene contains a directional pad sprite that the user uses to navigate the menu. In instruments It also confirms that their is no memory leaks, and it uses 4.79 mb of memory. Once again is that a reasonable amount? I will most likely switch to using AtlasSprite and AtlastSpriteManager to conserve memory.
I'm new to cocos2d, so if you see I'm doing anything wrong point it out! I'd rather fix bad habits in the early stages. And if you have any future tips for memory management please share.
Don't release logo, label, or background. You didn't alloc/copy/new/retain them, so you don't own them and must not release them.
I assume the controllerLayer property has the retain attribute? If not, you probably mean to do so.
In general I'd suggest two things going forward.
Read and understand the Cocoa Memory Management Fundamentals
Run the Clang analyzer on your code. This is available in Xcode 3.2 via Build->Build and Analyze. It will help detect these memory issues.
Also check out this SO question.