Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Thursday, January 13, 2011

A Sneaky post


Here are Matt's notes on using SneakyInput to make joysticks and buttons in your cocos2d-iphone game.
  • In the “HelloWorldScene.h”:
    • Import for buttons and JoyStick:
    • #import "SneakyJoystick.h"
    • #import "SneakyJoystickSkinnedBase.h"
    • #import "SneakyButton.h"
    • #import "SneakyButtonSkinnedBase.h"
    • Create a SneakyJoystick and SneakyButton (in interface):
      • SneakyJoystick* theJoystick;
      • SneakyButton* theButton;
    • After the interface, create a function that will make the buttons
      • -(void) makeControls;
  • In the “HelloWorldScene.m”
    • Define the function you created in the ‘.h’ file (makeControls)
      • You can copy/paste the makeControls function from your ShooterGame’s “HelloWorldScene.m” file
      • At this point, you will either need to reuse the images for the button/joystick or you can create your own graphics.
      • If you want to reuse the graphics, then you are going to have to track those down in the folder called “Shooter Graphics” or get them out of your spriteSheet you made for the Shooter Game project
      • If you want to make your own graphics, remember that you are going to have to make 2 graphics for the joystick (one that is the background image and the other is the joystick look) and then 3 graphics for your button (one for unpressed button, one for the pressed button, and one for the recently released button)
      • Put these graphics into a spritesheet and then import the ‘.plist’ file and the ‘.png’ image to your projects resources folder. Note! Don’t forget to check the box in the top left hand corner of the screen that pops up.
      • Once you have either reused or made new graphics, change the names in the code you copy/pasted to match the names of the graphics you have just imported
      • The very last thing you need to do is add to the init function of your “HelloWorldScene.m” file:
        • [self makeControls];
  • Example for making Joystick responding to movement
    • In the class you want to move with the controller (i.e. your character), first create a function called something like “moveBy” and define it:
      • -(void)moveBy:(CGPoint) amount;
      • CGSize size = [[CCDirector sharedDirector] winSize];
      • CGSize shipSize = playerShipSprite.contentSize;
      • CGPoint loc = playerShipSprite.position;
      • loc = ccpAdd(loc, amount);
      • if (loc.x < 0 + shipSize.width/4) {
      • loc.x = 0 + shipSize.width/4;}
      • if (loc.x > size.width - shipSize.width/4){
      • loc.x = size.width - shipSize.width/4;}
      • if (loc.y < 0 + shipSize.height/6){
      • loc.y = 0 + shipSize.height/6;}
      • if (loc.y > size.height - shipSize.height/6){
      • loc.y = size.height - shipSize.height/6;}
      • playerShipSprite.position = loc;
    • Then call this function in the Update of your HelloWorldScene.m file and determine what you want the “amount” part of the function to be
      • CGPoint change = ccpMult(theJoystick.stickPosition, delta*6);
      • [playerShip moveBy:change];
      • This “amount” is going to be how much the person will move each update call, the lower the number the more smooth, but slower he will move, so test it out to try and balance speed with smoothness
    • To get the button to do something when you push it do the following:
      • if (theButton.active) {
      • //put stuff in here to make the button do stuff}

No comments:

Post a Comment