如何限制触摸时的节点外观
How to limit nodes appearance on touch
我有一个名为 "sprite" 的节点,当我触摸屏幕时它在特定的 x 轴上显示 1 秒,之后 "sprite" 节点消失,我想让它在屏幕上只能同时有两个 "sprite" 个节点,它们不能在同一个位置。因为现在我可以添加 "sprite" 个节点,只要我的手指可以在屏幕上点击即可。这是我现在得到的:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[self addChild:sprite];
}
}
已编辑
@implementation GameScene
SKNode *groupSprite;
-(void)didMoveToView:(SKView *)view{
groupSprite = [[SKNode alloc]init];
[self addChild:groupSprite];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (groupSprite.children.count < 2) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 75);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = boardCategory;
sprite.physicsBody.contactTestBitMask = ballCategory;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
};
创建一个SKSpriteNode类型的数组。
var yourArray = [SKSpriteNode]
触摸时,在这个数组中添加新的节点。
yourArray.append(sprire)
当你的touch函数执行时,检查数组大小是否大于2。如果是,请不要做任何事情。
如果yourArray.count < 2 {
//你的代码
}
您可以像@kaizoku 指出的那样添加一个数组,但这只是一个浮动的变量,使代码可能难以管理。更好的方法是使用 SKNode 创建一个组,然后检查组数。
(我的objective c有点生疏,还请多多包涵)
...
SKNode *groupSprite = [[SKNode alloc] init];
...
-(void)didMoveToView:(SKView *)view
{
self.addChild(groupSprite);
}
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
if(groupSprite.children.count < 2)
{
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
}
}
}
您之所以要这样做,是因为它使您的代码更具可读性。你有一个场景,有一组精灵,其中包含精灵。现在,如果出于某种原因你想从屏幕上删除所有精灵。您只需将其从组中移除,您无需担心将其从场景中移除以及从数组中移除。
我有一个名为 "sprite" 的节点,当我触摸屏幕时它在特定的 x 轴上显示 1 秒,之后 "sprite" 节点消失,我想让它在屏幕上只能同时有两个 "sprite" 个节点,它们不能在同一个位置。因为现在我可以添加 "sprite" 个节点,只要我的手指可以在屏幕上点击即可。这是我现在得到的:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[self addChild:sprite];
}
}
已编辑
@implementation GameScene
SKNode *groupSprite;
-(void)didMoveToView:(SKView *)view{
groupSprite = [[SKNode alloc]init];
[self addChild:groupSprite];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (groupSprite.children.count < 2) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 75);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = boardCategory;
sprite.physicsBody.contactTestBitMask = ballCategory;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
};
创建一个SKSpriteNode类型的数组。
var yourArray = [SKSpriteNode]
触摸时,在这个数组中添加新的节点。
yourArray.append(sprire)
当你的touch函数执行时,检查数组大小是否大于2。如果是,请不要做任何事情。
如果yourArray.count < 2 { //你的代码 }
您可以像@kaizoku 指出的那样添加一个数组,但这只是一个浮动的变量,使代码可能难以管理。更好的方法是使用 SKNode 创建一个组,然后检查组数。
(我的objective c有点生疏,还请多多包涵)
...
SKNode *groupSprite = [[SKNode alloc] init];
...
-(void)didMoveToView:(SKView *)view
{
self.addChild(groupSprite);
}
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
if(groupSprite.children.count < 2)
{
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
}
}
}
您之所以要这样做,是因为它使您的代码更具可读性。你有一个场景,有一组精灵,其中包含精灵。现在,如果出于某种原因你想从屏幕上删除所有精灵。您只需将其从组中移除,您无需担心将其从场景中移除以及从数组中移除。