Game Development Reference
In-Depth Information
Adding a parallax background
As you may have noticed, we have huge grey windows in our background (see the
screenshot in the
Adding shield animations
section), and it would be great to add a
parallax background over it. We will do it the same way we did with the original
background. We will add new sprite nodes and move them every frame.
The things that need to be done to add a parallax background are as follows:
1.
Add
parallax.png
to
images.xcassets
in the Xcode project, either by
drag-and-drop or by clicking on the plus button in Xcassets. Select parallax
in the left Xcassets pane and move it from
1x
to
2x
by dragging, as we have
high-resolution artwork (see the following screenshot).
2.
The next thing we need is some variables in
Common.h
:
static NSString *parallaxName = @"parallax";
static NSInteger parallaxMoveSpeed = 10;
3.
We also need to have parallax as a property in
ERGMyScene.h
in order to
replace it with a new one. Add the following line:
@property (strong, nonatomic) ERGBackground *currentParallax;
4.
The next thing that we need to do is create that background layer. To do
that, go to the scene's
initWithSize:
method, and right after the creation
of
self.background
, add the code for the parallax layer's creation:
self.currentParallax = [ERGBackground generateNewParallax];
[self addChild:self.currentParallax];
5.
In order to generate a new parallax, we need to expand
ERGBackground.h
.
Add the following method definition there:
+ (ERGBackground *)generateNewParallax;