gpt4 book ai didi

sprite-kit - 检测哪个 SKSpriteNode 实例被触摸?

转载 作者:行者123 更新时间:2023-12-03 11:19:37 25 4
gpt4 key购买 nike

我正在使用这段代码来检测并查看用户点击是否在我的 SKSpriteNode 的框架内,如果是,则从屏幕上删除该节点。但我只想让被点击的节点消失。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];

if ((location.x > self.crate.frame.origin.x && location.x < self.crate.frame.origin.x + self.crate.frame.size.width) &&
(location.y > self.crate.frame.origin.y && location.y < self.crate.frame.origin.y + self.crate.frame.size.height)) {

[self.crate removeFromParent];
}
}
}

在我的更新方法中,我调用了一个方法 addCrate: 每秒生成一个节点。

- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {

self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 1) {
self.lastSpawnTimeInterval = 0;
[self addCrate];
}
}

- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}

[self updateWithTimeSinceLastUpdate:timeSinceLast];

}

这是它正在调用的方法。

- (void)addCrate {

// Create sprite
self.crate = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(30, 30)];
//self.crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.crate.frame.size];

// Determine where to spawn the crate along the X axis
int minX = self.crate.size.width / 2;
int maxX = self.frame.size.width - self.crate.size.width / 2;
int rangeX = maxX - minX;
int actualX = (arc4random_uniform(rangeX)) + minX;

// Create the crate slightly off-screen along the top,
// and along a random position along the X axis as calculated above
self.crate.position = CGPointMake(actualX, self.frame.size.height + self.crate.size.height/2);
[self addChild:self.crate];
self.crate.size = CGSizeMake(50, 50);


// Determine speed of the crate
int actualDuration = 3.5;

// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -self.crate.size.height/2) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[self.crate runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}

但是当我在我的 iPhone 上运行时,只是有时点击会被注册并且 block 会从屏幕上移除,有时则不会。同样,我希望被点击的节点消失,并且只有那个节点。

谢谢!

U1:

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {

self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
[self addCrate];
}
return self;
}

- (void)addCrate {

// Create sprite
self.crate = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(30, 30)];
self.crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 30)];
self.crate.userInteractionEnabled = YES;
//self.crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.crate.frame.size];

// Determine where to spawn the crate along the X axis
int minX = self.crate.size.width / 2;
int maxX = self.frame.size.width - self.crate.size.width / 2;
int rangeX = maxX - minX;
int actualX = (arc4random_uniform(rangeX)) + minX;

// Create the crate slightly off-screen along the top,
// and along a random position along the X axis as calculated above
self.crate.position = CGPointMake(actualX, self.frame.size.height + self.crate.size.height/2);
[self addChild:self.crate];
self.crate.size = CGSizeMake(50, 50);


// Determine speed of the crate
int actualDuration = 3.5;

// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -self.crate.size.height/2) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[self.crate runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
SKNode *touchedNode = [self nodeAtPoint:touchLocation];

NSLog(@"touchLocation x: %f and y: %f", touchLocation.x, touchLocation.y);

if (touchedNode != self) {
NSLog(@"Removed from parent.");
[touchedNode removeFromParent];
}
}


- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {

self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 1) {
self.lastSpawnTimeInterval = 0;
[self addCrate];
}
}

- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}

[self updateWithTimeSinceLastUpdate:timeSinceLast];

}

最佳答案

我认为您应该在创建 crate 时结合设置 node.name 属性并在 touchBegan: 方法中检查它们。

像这样:

SKSpriteNode *crate = [SKSpriteNode spriteNodeWithTexture:tex];
crate.name = @"crate";

还有touchBegan:方法:

.....
if ([touchedNode.name isEquelToString:@"crate"]){
// do something with that node
}
.....

更新1:

而不是写这些东西:

if ((location.x > self.crate.frame.origin.x && location.x < self.crate.frame.origin.x + self.crate.frame.size.width) &&
(location.y > self.crate.frame.origin.y && location.y < self.crate.frame.origin.y + self.crate.frame.size.height)) {

[self.crate removeFromParent];
}

使用:

if(CGRectContainsPoint(self.frame, touchPoint)){
// do something
}

Upd2:

在您的代码中看不到您正在 crate 节点上设置 userInteractionEnabled = YES。

Upd3:

这是一个例子:

//
// BGMyScene.m
// Test1
//
// Created by AndrewShmig on 3/10/14.
// Copyright (c) 2014 Bleeding Games. All rights reserved.
//

#import "BGMyScene.h"

@implementation BGMyScene

- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
/* Setup your scene here */

self.backgroundColor = [SKColor colorWithRed:0.15
green:0.15
blue:0.3
alpha:1.0];

// first label
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
// myLabel.userInteractionEnabled = YES;
myLabel.text = @"Hello, World!";
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];

// second label
SKLabelNode *myLabel2 = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
// myLabel2.userInteractionEnabled = YES;
myLabel2.text = @"Hello, World!";
myLabel2.fontSize = 30;
myLabel2.position = CGPointMake(100, 100);
[self addChild:myLabel2];
}
return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
SKNode *touchedNode = [self nodeAtPoint:touchLocation];

NSLog(@"touchLocation x: %f and y: %f", touchLocation.x, touchLocation.y);

if (touchedNode != self) {
NSLog(@"Removed from parent.");
[touchedNode removeFromParent];
}
}

- (void)update:(CFTimeInterval)currentTime
{
/* Called before each frame is rendered */
}

@end

您将看到以下屏幕: enter image description here

点击“Hello, World!”后它们将从父节点中删除的标签。

关于sprite-kit - 检测哪个 SKSpriteNode 实例被触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com