August 10, 2012 David Book buzztouch.com This is a companion file for the Custom Plugin Example - iOS. It's a simple plugin example but demonstrates the flexibility of the system nicely. Watch this video in the Plugins Learning Path playlist on the buzztouch YouTube Channel: http://www.youtube.com/user/buzztouchApp Then.... 1) Create your own Custom Plugin in your control panel. 2) Add it to an app you're working on. Do this by adding a new screen and choosing your new Plugin as the screen type. 3) Add the "bug image" and the "splat image" to your project. You can get these images here: Bug: http://www.buzztouch.com/images/bug.png Splat: http://www.buzztouch.com/images/splat.png 3) Copy / Paste the code in this file (below) into your plugin's .h and .m files --Rewind the video a bunch of times if you want, I go fast! BE SURE TO REPLACE "YOUR-PLUGIN-CLASS-FILE-NAME" with the name of your plugin. BE SURE TO REPLACE "YOUR-DELEGATE-NAME" with your app's delegate name. 4) Compile, run, smile. 5) Extend this simple idea by seeing if you can implement some additional methods found in most games. Some ideas include... -(void)showScore -(void)resetScore -(void)showTimer Super motivated folks may also see if they can modify the config.txt file for their app so it uses variables in the JSON (for the plugins screen) to allow dynamic values for things such as "bug speed", "bug image name", "start button text", etc. Feel free to look at all the other plugins for some ideas on how to do this. This is the code I copied and pasted in the .h file for this plugin... #################################################################### #import #import #import "BT_viewController.h" @interface YOUR-PLUGIN-CLASS-FILE-NAME : BT_viewController { UIImageView *imgViewBug; UIImageView *imgViewSplat; UIButton *buttonStart; NSTimer *gameLoopTimer; CGPoint destination; CGFloat xamt; CGFloat yamt; CGFloat speed; } @property (nonatomic, retain) UIImageView *imgViewBug; @property (nonatomic, retain) UIImageView *imgViewSplat; @property (nonatomic, retain) UIButton *buttonStart; @property (nonatomic, retain) NSTimer *timerGameLoop; @property (nonatomic) CGPoint destination; @property (nonatomic) CGFloat xamt; @property (nonatomic) CGFloat yamt; @property (nonatomic) CGFloat speed; -(void)startGame; -(void)endGame; -(void)moveBug; -(BOOL)intersecting:(CGPoint)touchLocation:(UIImageView *)imageViewBug; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; @end #################################################################### This is the code I copied and pasted in the .m file for this plugin... #################################################################### @implementation YOUR-PLUGIN-CLASS-FILE-NAME @synthesize imgViewBug, imgViewSplat, buttonStart, timerGameLoop; @synthesize destination, xamt, yamt, speed; //viewDidLoad -(void)viewDidLoad{ [BT_debugger showIt:self:@"viewDidLoad"]; [super viewDidLoad]; //put code here that adds UI controls to the screen. //create an image object for the bug... UIImage *imgBug = [UIImage imageNamed:@"bug.png"]; //setup bug image view, setting it's image to the bug... imgViewBug = [[UIImageView alloc] initWithImage:imgBug]; //add bug image to screen as subView [self.view addSubview:imgViewBug]; //set the starting frame for the bug (left, top, width, height)... [imgViewBug setFrame:CGRectMake(0,0,50,50)]; //hide the bug image until the game begins... [imgViewBug setHidden:TRUE]; //add the "splat" image on top of the bug. Make it exactly the same size as the bug. //this iamge is hidden until the bug is tapped... UIImage *imgSplat = [UIImage imageNamed:@"splat.png"]; imgViewSplat = [[UIImageView alloc] initWithImage:imgSplat]; [imgViewSplat setFrame:CGRectMake(0,0,50,50)]; [self.view addSubview:imgViewSplat]; [imgViewSplat setHidden:TRUE]; //create the start button... buttonStart = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [buttonStart setFrame:CGRectMake(80, 360, 160, 50)]; [buttonStart setTitle:@"Start Game" forState:UIControlStateNormal]; [buttonStart addTarget:self action:@selector(startGame) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:buttonStart]; //inti numeric values... destination = CGPointMake(0, 0); xamt = 0; yamt = 0; speed = 50; //lower number is faster } //view will appear -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [BT_debugger showIt:self:@"viewWillAppear"]; //flag this as the current screen YOUR-DELEGATE-NAME_appDelegate *appDelegate = (YOUR-DELEGATE-NAME_appDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.rootApp.currentScreenData = self.screenData; //setup navigation bar and background [BT_viewUtilities configureBackgroundAndNavBar:self:[self screenData]]; } //start game method... -(void)startGame{ [BT_debugger showIt:self:@"startGame"]; //hide the start button... [self.buttonStart setHidden:TRUE]; //set text on start button to "restart" for next time... [buttonStart setTitle:@"Play Again" forState:UIControlStateNormal]; //show the bug image... [imgViewBug setHidden:FALSE]; //make sure the splat is hidden... [imgViewSplat setHidden:TRUE]; //random location withing the 320 / 480 screen size... destination = CGPointMake(arc4random() % 320, arc4random() % 480); //calculate steps / moving distance based on speed... xamt = ((destination.x - self.imgViewBug.center.x) / speed); yamt = ((destination.y - self.imgViewBug.center.y) / speed); //fire timer every .02 milliseconds to trigger the "moveBug" method... gameLoopTimer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBug) userInfo:nil repeats: YES]; } //end game... -(void)endGame{ [BT_debugger showIt:self:@"endGame"]; //stop the timer... [gameLoopTimer invalidate]; gameLoopTimer = nil; //show the start button... [self.buttonStart setHidden:FALSE]; //hide the bug... [self.imgViewBug setHidden:TRUE]; //show the splat... [self.imgViewSplat setHidden:FALSE]; } //move bug... -(void)moveBug{ [BT_debugger showIt:self:@"moveBug"]; //tmp variables to calculate current distance... CGFloat xdist = fabs(destination.x - self.imgViewBug.center.x); CGFloat ydist = fabs(destination.y - self.imgViewBug.center.y); //if we are "close" to the destination, move the bug... if ( (xdist < 5) && (ydist < 5) ){ destination = CGPointMake(arc4random() % 320, arc4random() % 440); xamt = ((destination.x - self.imgViewBug.center.x) / speed); yamt = ((destination.y - self.imgViewBug.center.y) / speed); }else{ //keep moving if the distance is "too far"... self.imgViewBug.center = CGPointMake(self.imgViewBug.center.x + xamt, self.imgViewBug.center.y + yamt); self.imgViewSplat.center = CGPointMake(self.imgViewSplat.center.x + xamt, self.imgViewSplat.center.y + yamt); } } //checks if tap intersects bug image.. -(BOOL)intersecting:(CGPoint)touchLocation:(UIImageView *)imageViewBug{ //touch location, x and y... CGFloat x1 = touchLocation.x; CGFloat y1 = touchLocation.y; //square around bug image... CGFloat x2 = self.imgViewBug.frame.origin.x; CGFloat y2 = self.imgViewBug.frame.origin.y; CGFloat w2 = self.imgViewBug.frame.size.width; CGFloat h2 = self.imgViewBug.frame.size.height; //do they intersect? if ((x1 > x2) && (x1 < x2 + w2) && (y1 > y2) && (y1 < y2 + h2)) { return TRUE; }else{ return FALSE; } } //capture touch events... -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //touch object passed in by iOS... UITouch *touch = [[event allTouches] anyObject]; //the location of the touch on the screen... CGPoint location = [touch locationInView:self.view]; //if the touch was "on the bug", stop the bug... if ([self intersecting:location :self.imgViewBug]) { //end the game... [self endGame]; } } //dealloc -(void)dealloc { [super dealloc]; [imgViewBug release]; [imgViewSplat release]; [buttonStart release]; [timerGameLoop release]; } @end ####################################################################