我正在为我的游戏创建一个虚拟游戏手柄,它有一个带有圆形图像的主要SKSpriteNode和带有图像的四个子节点(上,下,左,右)。当我使用默认大小时,它看起来很完美,但它的大小很大,当我尝试调整主圆SKSpriteNode的大小时,所有它的子节点都比它的父节点大,我就是无法计算适合每个条件(不同大小的主节点)的子节点的大小。
-(id)initWithJoystickImage:(NSString *)baseImage
upImage:(NSString *)upImage downImage:(NSString *)downImage leftImage:(NSString *)leftImage rightImage:(NSString *)rightImage
{
if((self = [super initWithImageNamed:baseImage]))
{
SKSpriteNode *up = [[SKSpriteNode alloc] initWithImageNamed:upImage];
up.position = CGPointMake(0, up.frame.size.height/2);
[self addChild:up];
SKSpriteNode *down = [[SKSpriteNode alloc] initWithImageNamed:downImage];
down.position = CGPointMake(0, -down.frame.size.height/2);
[self addChild:down];
SKSpriteNode *right = [[SKSpriteNode alloc] initWithImageNamed:rightImage];
right.position = CGPointMake(right.frame.size.width/2, 0);
self.size.height/2.5);
[self addChild:right];
SKSpriteNode *left = [[SKSpriteNode alloc] initWithImageNamed:leftImage];
left.position = CGPointMake(-left.frame.size.width/2, 0);
self.size.height/2.5);
[self addChild:left];
}
return self;
}
使用默认尺寸时
根据设备尺寸调整大小
编辑1:添加操纵杆并调整代码大小
Joystick *joystick = [[Joystick alloc] initWithJoystickImage:@"cricle.png" upImage:@"up.png" downImage:@"down.png" leftImage:@"left.png" rightImage:@"right.png"];
joystick.size=CGSizeMake(DeviceHeight/3, DeviceHeight/3);
joystick.position=CGPointMake(DeviceWidth/2-joystick.frame.size.width/2, -DeviceHeight/2+joystick.frame.size.height/2);
答案 0 :(得分:2)
您只能更改父节点(joystick
)的大小。这将一起调整父节点和子节点的大小:
[joystick runAction:[SKAction scaleTo:2.0/3.0 duration:0.0]]; // Change scale value