This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
extensions:be_parallax_sprite [2011/03/15 03:26] – enbr | extensions:be_parallax_sprite [2015/09/14 11:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== BEParallaxSprite ====== | ||
+ | |||
+ | ---- dataentry extension ---- | ||
+ | type : extension | ||
+ | author_mail | ||
+ | description | ||
+ | lastupdate_dt : 2011-01-29 | ||
+ | compatible | ||
+ | depends | ||
+ | tags : parallax, scrolling, background, layers | ||
+ | homepage_url | ||
+ | download_url | ||
+ | ---- | ||
+ | \\ [[/ | ||
+ | \\ | ||
+ | ===== Usage ===== | ||
+ | |||
+ | {{http:// | ||
+ | |||
+ | <code objc> | ||
+ | sky = [BEParallaxSprite parallexSpriteWithTexture: | ||
+ | [self addChild: | ||
+ | </ | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 2011/01/23: First public version | ||
+ | * 2011/01/29: Added start and stop methods | ||
+ | ===== Source Code ===== | ||
+ | |||
+ | BEParallaxSprite.h | ||
+ | <code objc> | ||
+ | #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: | ||
+ | - (void)start; | ||
+ | - (void)stop; | ||
+ | - (id)initWithTexture: | ||
+ | - (id)initWithTexture: | ||
+ | - (id)initWithTexture: | ||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | BEParallaxSprite.m | ||
+ | <code objc> | ||
+ | #import " | ||
+ | |||
+ | @implementation BEParallaxSprite | ||
+ | |||
+ | @synthesize speed = mSpeed; | ||
+ | @synthesize running = mRunning; | ||
+ | |||
+ | - (id)initWithTexture: | ||
+ | if (self = [super init]) { | ||
+ | [self initWithTexture: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (id)initWithTexture: | ||
+ | if (self = [super init]) { | ||
+ | [self initWithTexture: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (id)initWithTexture: | ||
+ | if (self = [super init]) { | ||
+ | mRunning = YES; | ||
+ | mDirection = direction; | ||
+ | if (direction < 1 || direction > 4) { | ||
+ | mDirection = BE_PARALLAX_DIRECTION_LEFT; | ||
+ | } | ||
+ | mSpeed = speed; | ||
+ | mImage1 = [SPImage imageWithTexture: | ||
+ | [self addChild: | ||
+ | mImage2 = [SPImage imageWithTexture: | ||
+ | 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: | ||
+ | [self addEventListener: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | return [[[BEParallaxSprite alloc] initWithTexture: | ||
+ | } | ||
+ | |||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | return [[[BEParallaxSprite alloc] initWithTexture: | ||
+ | } | ||
+ | |||
+ | + (BEParallaxSprite *)parallexSpriteWithTexture: | ||
+ | return [[[BEParallaxSprite alloc] initWithTexture: | ||
+ | } | ||
+ | |||
+ | - (void)start { | ||
+ | if (mRunning != YES) { | ||
+ | mRunning = YES; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | - (void)stop { | ||
+ | if (mRunning != NO) { | ||
+ | mRunning = NO; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | - (void)onEnterFrame: | ||
+ | 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 " | ||
+ | It took me a lot of time to track down it was missing, causing linker errors. |