Dpad for sprite kit

梦想的初衷 提交于 2019-12-06 11:02:00

问题


I an interested in trying to create a few games, namely in the new sprite kit. However, for the game that I have in mind I would rather use a directional pad vs a joy stick. Since I shall be moving over from Cocos, my old program no longer works (so neither will that dpad). Sadly, I have not come accross any tutorial that may help with implementing one in the new sprite kit (only joy sticks). I was wondering and hoping if anyone came across a simple dpad class from github or something or a helpful link that they would like to share. Thanks


回答1:


Here's one way:

//Joystick node
- (SKSpriteNode *)controlPadNode
{
    SKSpriteNode *controlPadNode = [SKSpriteNode spriteNodeWithImageNamed:@"game-controller-front"];
    controlPadNode.position = CGPointMake(controlPadX,controlPadY);
    controlPadNode.name = @"controlPadNode";
    controlPadNode.zPosition = 1.0;
    return controlPadNode;
}

//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        touchX = location.x;
        touchY = location.y;
        controlButtonDown = true;
    }
}

//when touch moves
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        touchX = location.x;
        touchY = location.y;
        controlButtonDown = true;
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        controlButtonDown = false;
    }
}

//update method
-(void)update:(NSTimeInterval)currentTime {

    if (controlButtonDown) {

        //the control pad
        SKNode *controlNode = [self childNodeWithName:@"controlPadNode"];

        //the node you want to move
        SKNode *node = [self childNodeWithName:@"spaceShipNode"];

        //compute the angle between parameters pad and the horizontal
        float angle = atan2f (touchY - controlPadY, touchX - controlPadX) ;

        //move the control pad for the player to have a sense of movement
        SKAction *moveControlPad = [SKAction moveTo:CGPointMake(touchX, touchY) duration:0.00001];
        double distance = sqrt(pow((touchX - controlPadX), 2.0) + pow((touchY - controlPadY), 2.0));

        //make sure the control pad only moves 40 pixels
        if( distance < 40 ){
            [controlNode runAction:moveControlPad];
        }

        //move the ship
        SKAction *moveShip = [SKAction moveByX: 6*cosf(angle) y:6*sinf(angle) duration:0.005];
        [node runAction: moveShip];        

    }
}

EDIT:

Also can use this to move the control pad back when touch ends:

if (!controlButtonDown) {
    SKNode *controlNode = [self childNodeWithName:@"controlPadNode"];
    SKAction *moveControlPad = [SKAction moveTo:CGPointMake(controlPadX, controlPadY) duration:0.00001];
    [controlNode runAction:moveControlPad];
}



回答2:


I've built a small UIKit based game engine before the introduction of SpriteKit. My engine loads cocos2d compatible maps from Tiled, sprites from TexturePacker, and it comes with a joystick that I ported from cocos2d to UIKit. (Mine is based on sneaky-joystick.)

You can grab the whole library on GitHub and pull out the parts you need. It's fairly modular.



来源:https://stackoverflow.com/questions/21152523/dpad-for-sprite-kit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!