//
// cloneParallax.h
#import <SpriteKit/SpriteKit.h>
@interface cloneParallax : SKSpriteNode
{
}
-(id) initParallax:(float)PtotalWidth parallaxSpeed:(float)Pspeed parallaxY:(float)Py;
//properties
@property float PtotalWidth;
@property SKTexture *Ptexture;
@property float RXY;
@property float Pspeed;
@property float Py;
@property float clonewidth;
-(void)DrawParallax:(SKTexture*)Ptexture scaleXY:(float)R;
-(void)DrawParallaxSprites:(NSMutableArray*)textureArray widthofSprite:(float)_width scaleXY:(float)R;
-(void) updateParallax:(CFTimeInterval)interval;
@end
//
//
// cloneParallax.m
// milkhunt
//
// Created by Manish Chauhan on 26/12/13.
// Copyright (c) 2013 skidos. All rights reserved.
//
#import "cloneParallax.h"
#import "sprite.h"
@implementation cloneParallax
{
int numberofclones;
SKSpriteNode *realsprite;
sprite *realanimation;
}
-(id) initParallax:(float)PtotalWidth parallaxSpeed:(float)Pspeed parallaxY:(float)Py
{
self=[super init];
if(self)
{
_Py=Py;
_PtotalWidth=PtotalWidth;
_Pspeed=Pspeed;
}
return self;
}
//if you are looking for plain sprite parallax
-(void)DrawParallax:(SKTexture*)Ptexture scaleXY:(float)R
{
_RXY=R;
_Ptexture=Ptexture;
realsprite=[SKSpriteNode spriteNodeWithTexture:_Ptexture];
_clonewidth=realsprite.size.width*_RXY;
numberofclones=ceil((_PtotalWidth+_Pspeed)/realsprite.size.width);
numberofclones++;
[self fillParallax];
}
//if you are looking for animated sprite parallax such as water or animated clouds etc
-(void)DrawParallaxSprites:(NSMutableArray*)textureArray widthofSprite:(float)_width scaleXY:(float)R
{
_RXY=R;
realanimation=[[sprite alloc] init];
[realanimation runAnimation:textureArray type:@"loop"];
_clonewidth=_width;
numberofclones=ceil((_PtotalWidth+_Pspeed)/_width);
numberofclones++;
[self fillParallaxSprites];
}
-(void)fillParallaxSprites
{
for(int i=0;i<numberofclones;i++)
{
sprite *clonesprite=[realanimation copy];
clonesprite.speed=16;
//clonesprite.xScale=clonesprite.yScale=_RXY;
clonesprite.position=CGPointMake(i*(_clonewidth-1), _Py);
[self addChild:clonesprite];
}
}
-(void)fillParallax
{
for(int i=0;i<numberofclones;i++)
{
SKSpriteNode *clonesprite=[realsprite copy];
clonesprite.anchorPoint=CGPointMake(0, 0);
clonesprite.xScale=clonesprite.yScale=_RXY;
clonesprite.position=CGPointMake(i*(_clonewidth), _Py);
[self addChild:clonesprite];
}
}
-(void) updateParallax:(CFTimeInterval)interval
{
float oldX=(self.position.x-_Pspeed);
float newX = (float)((int)oldX % (int)_clonewidth);
self.position=CGPointMake(newX, _Py);
}
@end
//adding clone parallax to your main class
@implementation milkhuntStartScene
{
cloneParallax *waterParallax;
}
waterParallax=[[cloneParallax alloc] initParallax:1024 parallaxSpeed:2 parallaxY:30];
// [waterParallax DrawParallax:your texture scaleXY:1];//for simple sprite
// [waterParallax DrawParallaxSprites:your texture array widthofSprite:width of each texture scaleXY:1];//for animated sprite
[self addChild:waterParallax];
//update clone parallax with speed
-(void)update:(NSTimeInterval)currentTime
{
[waterParallax updateParallax:currentTime];
}
Comments
Post a Comment