~~NOTOC~~ ====== BEParallaxSprite ====== ---- dataentry extension ---- type : extension author_mail : briancensor@gmail.com Brian Ensor description : A simple parallax background sprite. lastupdate_dt : 2011-01-29 compatible : v1.1 depends : tags : parallax, scrolling, background, layers homepage_url : https://github.com/brianensorapps/BEParallaxSprite download_url : https://github.com/brianensorapps/BEParallaxSprite/zipball/master ---- \\ [[/users/brian/start|My Wiki Page]] \\ ===== Usage ===== {{http://img860.imageshack.us/img860/784/screenshot20110313at114.png|}} sky = [BEParallaxSprite parallexSpriteWithTexture:[SPTexture textureWithContentsOfFile:@"sky.png"] speed:0.5 direction:BE_PARALLAX_DIRECTION_LEFT]; [self addChild:sky]; ===== Changelog ===== * 2011/01/23: First public version * 2011/01/29: Added start and stop methods ===== Source Code ===== BEParallaxSprite.h #import #define BE_PARALLAX_DIRECTION_LEFT 1 #define BE_PARALLAX_DIRECTION_RIGHT 2 #define BE_PARALLAX_DIRECTION_UP 3 #define BE_PARALLAX_DIRECTION_DOWN 4 @interface BEParallaxSprite : SPSprite { SPImage *mImage1; SPImage *mImage2; float mSpeed; int mDirection; float mCurStep; bool mRunning; } @property (nonatomic, assign) bool running; @property (nonatomic, assign) float speed; - (void)onEnterFrame:(SPEnterFrameEvent *)event; - (void)start; - (void)stop; - (id)initWithTexture:(SPTexture *)texture; - (id)initWithTexture:(SPTexture *)texture speed:(float)speed; - (id)initWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction; + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture; + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed; + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction; @end BEParallaxSprite.m #import "BEParallaxSprite.h" @implementation BEParallaxSprite @synthesize speed = mSpeed; @synthesize running = mRunning; - (id)initWithTexture:(SPTexture *)texture { if (self = [super init]) { [self initWithTexture:texture speed:1 direction:BE_PARALLAX_DIRECTION_LEFT]; } return self; } - (id)initWithTexture:(SPTexture *)texture speed:(float)speed { if (self = [super init]) { [self initWithTexture:texture speed:speed direction:BE_PARALLAX_DIRECTION_LEFT]; } return self; } - (id)initWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction { if (self = [super init]) { mRunning = YES; mDirection = direction; if (direction < 1 || direction > 4) { mDirection = BE_PARALLAX_DIRECTION_LEFT; } mSpeed = speed; mImage1 = [SPImage imageWithTexture:texture]; [self addChild:mImage1]; mImage2 = [SPImage imageWithTexture:texture]; if (mDirection == BE_PARALLAX_DIRECTION_DOWN) { mImage2.y = mImage1.y-mImage2.height; } if (mDirection == BE_PARALLAX_DIRECTION_UP) { mImage2.y = mImage1.y+mImage1.height; } if (mDirection == BE_PARALLAX_DIRECTION_RIGHT) { mImage2.x = mImage1.x-mImage2.width; } if (mDirection == BE_PARALLAX_DIRECTION_LEFT) { mImage2.x = mImage1.x+mImage1.width; } [self addChild:mImage2]; [self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME]; } return self; } + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture { return [[[BEParallaxSprite alloc] initWithTexture:texture speed:1 direction:BE_PARALLAX_DIRECTION_LEFT] autorelease]; } + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed { return [[[BEParallaxSprite alloc] initWithTexture:texture speed:speed direction:BE_PARALLAX_DIRECTION_LEFT] autorelease]; } + (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction { return [[[BEParallaxSprite alloc] initWithTexture:texture speed:speed direction:direction] autorelease]; } - (void)start { if (mRunning != YES) { mRunning = YES; } } - (void)stop { if (mRunning != NO) { mRunning = NO; } } - (void)onEnterFrame:(SPEnterFrameEvent *)event { if (mRunning == YES) { mCurStep += mSpeed; if (mCurStep < 1) return; if (mDirection == BE_PARALLAX_DIRECTION_DOWN) { mImage1.y += floor(mCurStep); mImage2.y += floor(mCurStep); if (mImage1.y >= mImage1.height) { mImage1.y = mImage2.y-mImage2.height; } if (mImage2.y >= mImage2.height) { mImage2.y = mImage1.y-mImage1.height; } } if (mDirection == BE_PARALLAX_DIRECTION_UP) { mImage1.y -= floor(mCurStep); mImage2.y -= floor(mCurStep); if (mImage1.y <= -mImage1.height) { mImage1.y = mImage2.y+mImage2.height; } if (mImage2.y <= -mImage2.height) { mImage2.y = mImage1.y+mImage1.height; } } if (mDirection == BE_PARALLAX_DIRECTION_RIGHT) { mImage1.x += floor(mCurStep); mImage2.x += floor(mCurStep); if (mImage1.x >= mImage1.width) { mImage1.x = mImage2.x-mImage2.width; } if (mImage2.x >= mImage2.width) { mImage2.x = mImage1.x-mImage1.width; } } if (mDirection == BE_PARALLAX_DIRECTION_LEFT) { mImage1.x -= floor(mCurStep); mImage2.x -= floor(mCurStep); if (mImage1.x <= -mImage1.width) { mImage1.x = mImage2.x+mImage2.width; } if (mImage2.x <= -mImage2.width) { mImage2.x = mImage1.x+mImage1.width; } } mCurStep -= floor(mCurStep); } } - (void)dealloc { [super dealloc]; } @end If using Xcode 4 and the latest sparrow release, remember to include "libz.dylib" in your project. It took me a lot of time to track down it was missing, causing linker errors.