Hi all, i'm very new at this so I might be doing something silly but i'm having trouble tweening my images. I completed the beginner's tutorial posted by ManiacDev, and the game i'm working on is similar except instead of balloons floating upwards, we're trying to get bugs flying across the screen (landscape orientation).
While the balloon tutorial did everything in the Game class, i'm trying to break our game down into several classes like in the Demo game. So I have the Game holding the main menu, a class for each level, and a class for the player. At the moment there's only one level and it's into this class that i'm trying to incorporate similar tweening code to the balloon tutorial. This class inherits from SPSprite.
The problem is the bugs are being placed on the screen but they don't move. I get an error 'request for member juggler in something not a structure or a union' if I try to add the tween to the juggler using [self.juggler addObject:tween] like the Balloon Tutorial does. I presume this has to do with the class not having a juggler component or something? Following the example in the Docs, changing this to [self.stage.juggler addObject: tween] removes the error, but the bugs do not move.
I'm confused because I have a similar tween working for my Player class, although that is initiated from a touch event.
The relevant code from my "Green Land" level class is below:
GreenLand.h
#import "Sparrow.h"
@interface GreenLand : SPSprite
{
// declare an array (mutable means it can be changed at runtime) to hold the bugs images
NSMutableArray *bugsTextures;
// declare a sprite - a display object container - to hold the bugs images during play
SPSprite *playFieldSprite;
}
// declare addBug method - returns nothing
-(void) addBug;
// declare a method to draw the bugs (calls the onTouchBalloon method repeatedly)
-(void) drawBugs;
@end
GreenLand.m
#import "GreenLand.h"
@interface GreenLand ()
@end
// --- class implementation ------------------------------------------------------------------------
@implementation GreenLand
- (id)init {
if (self = [super init]) {
// initialise the array
bugsTextures = [NSMutableArray array];
// load the bugs images into the array as textures
// using SPTexture rather than SPImage so that we can easily make images
// from the textures throughout the class
[bugsTextures addObject:[SPTexture textureWithContentsOfFile:@"fly.png"]];
[bugsTextures addObject:[SPTexture textureWithContentsOfFile:@"ladybird.png"]];
// keep this array in memory
[bugsTextures retain];
// initialise the sprite to hold the bugs images (the playing field)
playFieldSprite = [SPSprite sprite];
// add the sprite to the screen
[self addChild: playFieldSprite];
[self addBug];
}
return self;
}
-(void) addBug
{
// declare and initialise an image to hold a random texture from the bugsTextures array
SPImage *bugImg = [SPImage imageWithTexture: [bugsTextures objectAtIndex:arc4random() % bugsTextures.count]];
// set the x co-ordinate of the image on the stage
bugImg.x = self.width-200;
// set the y co-ordinate of the image to a random value within the height of the stage
bugImg.y = (arc4random() % (int)(self.height-bugImg.height));
// add the image as a child of the playFieldSprite
[playFieldSprite addChild: bugImg];
// move the bug left using the SPTween class
// declare and initialise a tween by providing the SPTween class with the object to tween
// the time to take, and the transition type to use
SPTween *tween = [SPTween tweenWithTarget:bugImg time:(double)((arc4random() % 5)+ 2) transition:SP_TRANSITION_LINEAR];
// define the properties the tween will animate (bug's position)
// the x co-ordinate will be modified to minus the width of the bug sprite
// in other words, it will constantly move left for the duration of the tween
[tween animateProperty:@"x" targetValue:-bugImg.width];
// the y co-ordinate will be modified using a random value within the range
// of the width of the stage minus the width of the bug sprite
[tween animateProperty:@"y" targetValue:arc4random() % (int)(self.width-bugImg.width)];
// add the tween object to the juggler for the stage we are performing the tween on
// the juggler handles timing for animation
[self.stage.juggler addObject:tween];
}
- (void)dealloc {
[super dealloc];
}
@end
I would appreciate any help anyone can give.
I'm very aware that i'm probably making numerous mistakes, both in my coding syntax and my approach, so I sincerely apologise. I also realise that having a separate class for every level is not very efficient (at least, it doesn't seem very efficient to me!). My aim at the moment is to get this working, then look at how I can abstract it out to a generic level class that loads different classes (such as a bugs class, and the player class) depending on the level the player is on. At least, thats seems a wise approach to me, but I still have a huge amount to learn about Objective-C and Object Oriented Programming in general. I'm trying to learn as I go, at least until this project is out the way, then I can slow down and start at the very beginning.