Porting a project to Android

We will port the Swift project we created in the last chapter to Android.

The process of creating the project in SpriteBuilder is the same as in Swift; however, obviously, this time, the code needs to be in Objective-C.

How to do it…

Once the project is created in SpriteBuilder, we can open the project in Xcode 6.1.1 and even run the project to make sure there are no build errors.

The built project should run on our device as shown in the following screenshot:

How to do it…

Now, as in the Swift project, we have to create a Rocket class. So, we create a new class called Rocket. The Rocket.h and Rocket.m files should look similar to the following:

//Rocket.h

#import "CCSprite.h"

@interface Rocket : CCSprite{

  CGSize winSize;


}


@end

The Rocket.m file should be as follows. Note that how we position the rocket is different here as compared to Swift:

#import "Rocket.h"

@implementation Rocket:CCSprite


-(void) didLoadFromCCB {

  NSLog(@"%@ did load", self);

  winSize = [[CCDirector sharedDirector]viewSize];
}

-(void)update:(CCTime)delta{

  NSLog(@"[rocket] (update) ");

  if(self.position.x > winSize.width){

    [self removeFromParent];

  }else{


    CGPoint position = ccpAdd(self.position, CGPointMake(5.0, 0.0));
    [self setPosition:position];

  }
}


@end

Next, we also need to make changes to the MainScene.h and MainScene.m files, as follows:

//MainScene.h
@interface MainScene : CCNode{

  CGSize winSize;
  CCSprite* hero;


}


@end

Now, we will add the code for the MainScene.m file.

How to do it…

Note that the function call remains the same while loading the rocket. We will now run the following:

#import "MainScene.h"

#import "Rocket.h"

@implementation MainScene

- (void)onEnter
{
  [super onEnter];
  self.userInteractionEnabled = YES;

}

- (void)onExit
{
  [super onExit];
  self.userInteractionEnabled = NO;

}


-(id)init{

  if(self = [super init]){

    winSize = [[CCDirector sharedDirector]viewSize];
    CGPoint center = CGPointMake(winSize.width/2, winSize.height/2);

  }

  return  self;
}


-(void)update:(CCTime)delta{

  //NSLog(@"[MainScene] (update) ");
}


- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{

  CCLOG(@"TOUCHES BEGAN");

  CGPoint touchLocation = [touch locationInNode:self];
  hero.position = touchLocation;


  Rocket* rocket = (Rocket*)[CCBReader load:@"Assets/rocket"];
  rocket.position = hero.position;
  [self addChild:rocket];


}

 - (void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{

  CCLOG(@"TOUCHES MOVED");

  CGPoint touchLocation = [touch locationInNode:self];
  hero.position = touchLocation;

}

 - (void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event{

  CCLOG(@"TOUCHES ENDED");

}



@end

The project structure should look similar to the following:

How to do it…

Now, when we finally build the project, the game should run exactly how it ran while we ran the Swift project.

When we tap the screen, the hero should move to this location and a rocket should fire. Once the rocket goes offscreen, it will delete itself.

How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.78.136