Skip to main content

Posts

working with blocks and function which takes block as a parameter in objective c

how to create a block in objective c define a block in .h file  /*here i created a block  addPhysicsBody is the name of block ^ without is block can’t be completed  so block is defined ^addPhysicsBody typedef void (^addPhysicsBody)( CGPoint ); where void is return type and CGpoint is parameter where bolded part is a parameter taken by block 2)  step 2 @property (copy) addPhysicsBody clickBlock; add block as a property and please remember that this property is copy   because a block needs to be copied to keep track of its captured state outside of the original scope by apple  define a function in .h file that takes block as a parameter this is necessary while creating block with function  - (void)getPhysicsBody:( void (^)(CGPoint) )finishBlock; where above bolded part is block with its para...

moving from one UIViewController to another from sprite kit SKScene

create a new  NSNotificationCenter inside your  UIViewController class inside  viewDidAppear function  1). -( void )viewDidAppear:( BOOL )animated {     [[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (disMissViewController) name : @"disMissViewController"   object : nil ]; } 2.) create selector  -( void )disMissViewController{         //in case of story board     UIStoryboard *storyboard = [ UIStoryboard storyboardWithName : @"Main"   bundle : nil ];     mainView = [storyboard instantiateViewControllerWithIdentifier : @"main" ];     //in case of code       mainView =[[ UIViewController alloc ] init ];     [ self presentViewController : newview Controller   animated : YES completion :^{              }];     //remove...

handling multiple sprite animation in cocos2dx3.2

many times in gaming world we need to handle multiple animation for e.g. when hero is running we have to call running sprite or frames when hero is jumping we need to call jumping frame here is my solution for that ==================== .h file // //  spriteSheet.h //  firegame // //  Created by Manish on 22/03/15. // // #ifndef __firegame__spriteSheet__ #define __firegame__spriteSheet__ #include "cocos2d.h" class spriteSheet : public cocos2d :: SpriteBatchNode {     public :         //constructor         spriteSheet();         //destructor         ~spriteSheet();         static spriteSheet * create( /*spritesheet reference*/ std :: string __spriteReference, /*plist reference */ std :: string __plistname, /*first frame of spritesheet*/ std :: string firstframe);             void init...

creating a endless parallax scrolling background in spritekit and objective c

// //  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 numberofcl...

Algorithm for generating weighed random numbers in objective c

-( int ) randomIndexByWeights:( NSArray * )weights {     // add weights     int weightsTotal = 0 ;          for ( int i = 0 ; i < [weights count ]; i++ )     {         weightsTotal+= [[weights objectAtIndex : i] intValue ];              }     float randomNum = (( float ) rand () / RAND_MAX ) * 1 ;     int rand =( int )(randomNum * weightsTotal);     // step through array to find where that would be     weightsTotal = 0 ;     for ( int x = 0 ; x < [weights count ]; x++ )     {                  weightsTotal+= [[weights objectAtIndex : x] intValue ];                  if ( weightsTotal >= rand)         {             return x; ...

Creating a SpriteBatchNode subclass for sprite animation

SpriteBatchNode is one of the most usefull class of cocos2d-x-3.2 Now here I am creating a subclass which is extend from SpriteBatchNode You .h file #ifndef _ANIMALANIMATION_H_ #define _ANIMALANIMATION_H_ #include "cocos2d.h" class animalAnimation : public cocos2d:: SpriteBatchNode {     public :                       animalAnimation();         ~animalAnimation();         static animalAnimation * create(std:: string spriteReference,std:: string plistname,std:: string firstframe, int frames);         void initOptions(std:: string plistname,std:: string firstframe, int frames);               }; #endif // Now .cpp file #include "animalAnim...