HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Classes for background layer and parallax effect

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
effectlayerbackgroundparallaxforclassesand

Problem

I have two classes DigBackground and DigParallaxBackgroundLayer (a parallax background is a image consisting of several images that move at different scroll speeds, for games etc.). My idea was to have the DigBackground class which is basically just a image with some added properties that can be set were it will draw itself on the screen. It just has one method currently and that is the initWithImageNamed that sets all of it properties. Some of its properties are public, but read-only. So they can be accessed by the DigParallaxBackground class.

The DigParallaxBackground class currently also have one method at present - the initializer which takes an array of DigBackground´s as argument. And will display them on screen.

I want to know if this is decent design and how I can improve it if need be.

This is how it's used:

DigBackground *bg1 = [[DigBackground alloc]initWithImageNamed:@"Default.png"
                                                  withScrollSpeed:1.0f
                                                  InitialOffsetOf:ccp(0, 0)
                                                         position:ccp(100, 100)
                                                         setFlipX:NO];

    DigParallaxBackgroundLayer *parallaxBg = [[DigParallaxBackgroundLayer alloc]
                                              initParallaxBackgroundWithImages:@[bg1] inOrder:kAscending];

    [self addChild: parallaxBg];


Header for DigBackground:

@property (nonatomic, readonly) float bgScrollSpeed;
@property (nonatomic, readonly) CGPoint initialOffset;

// Designated initializer
- (id) initWithImageNamed: (NSString *)image
          withScrollSpeed: (float)scrollspeed
          InitialOffsetOf: (CGPoint)offset
                 position: (CGPoint)position
                 setFlipX: (BOOL)flipX;


Header for DigParallaxBackground:

```
typedef enum {
kAscending,
kDescending
} ImagesOrder;

@interface DigParallaxBackgroundLayer : CCLayer

Solution

Just a couple nit-picky things...

InitialOffsetOf: should instead be initialOffset:

withScrollSpeed: should just be scrollSpeed:

I'd probably also add factory methods. When I'm writing code, no class is complete until it has its factory methods.

Context

StackExchange Code Review Q#27018, answer score: 5

Revisions (0)

No revisions yet.