我一直在尝试制作一个简单的应用,当你点击一个圆圈时,它会消失,一个新的会出现在其他地方。
这是我编辑的所有代码,但它仍然无效。
#import "GameScene.h"
@implementation GameScene
-(void)didMoveToView:(SKView *)view {
[self LabelShow];
}
int Count = 0;
int CountCheck = 0;
-(void) LabelShow {
//Nothing yet
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
while (CountCheck == Count) {
int FNum1 = 350;
int TNum1 = 650;
int newx = (arc4random()%(TNum1-FNum1))+FNum1;
NSLog(@"RandomXCoord: %i", newx);
int FNum2 = 100;
int TNum2 = 700;
int newy = (arc4random()%(TNum2-FNum2))+FNum2;
NSLog(@"RandomYCoord: %i", newy);
//Location to newx and newy
CGPoint location = CGPointMake(newx, newy);
SKSpriteNode *Circle = [SKSpriteNode spriteNodeWithImageNamed:@"Circle.png"];
Circle.xScale = 0.2;
Circle.yScale = 0.2;
Circle.position = location;
//Shows chircle
[self addChild:Circle];
CountCheck++;
for (UITouch *touch in touches)
{
CGPoint locationTouch = [touch locationInNode:self];
if ([Circle containsPoint:locationTouch]) {
NSLog(@"Success!");
[Circle runAction:[SKAction fadeOutWithDuration:0]];
Count++;
}
}
}
}
@end
正如我所说的,我有一个代码,用另一种方法将圆圈放在屏幕上。但每当我运行此代码(单击圆圈)时,底部的if语句都不会被执行。
我尝试了这个帖子中的所有内容: Can't tap SKSpriteNode - no touch detected ios ,但我无法让它继续工作,并已将代码更改回原始代码。
有人能告诉我当你点击SKSpriteNode时我怎么能够执行if语句?
感谢您阅读此问题。
编辑我更改了代码,但仍然无效
答案 0 :(得分:1)
在Circle.xScale = 0.2;
声明
Circle.name = @"Circle";
并将for-loop
替换为以下
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"Circle"]) {
NSLog(@"Success!");
[node removeFromParent];
++Count;
}
}
答案 1 :(得分:0)
您需要的是containsPoint
方法。
以下是您要查找的touchesBeganWithEvent:
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInNode:self];
if ([circle containsPoint:location] {
// executed when you touch a circle
}
}
}
请注意,您可以添加更多语句和条件。
答案 2 :(得分:0)
我为自己创造了一个游戏,我必须为其中的角色做同样的事情。我将SKNode子类化,并在子类模型中添加了角色细节(SKSpriteNode,name,SKAction等)。然后我将UITouchBegin方法添加到子类中,因此我的角色会听取触摸。
使用子类化要容易得多,因为您可以将所有的工作留给子类,并专注于游戏的其余部分。将此代码添加到您的子类将负责所有:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self removeFromParent];
}
您甚至可以将场景设置为角色类的委托,一旦从父视图中删除角色,您就可以创建一个新角色。
如果您遇到子类问题,this回答确实帮助了我。如果有任何疑问和好运,请告诉我:)
答案 3 :(得分:0)
其他响应似乎检测到SKSpriteNode上的触摸,而不是点击。此代码检测点击事件。
修改TapMaxDelta
以更改在取消点按手势之前允许的移动量。
class TapNode : SKSpriteNode {
// Tap Vars
var firstPoint : CGPoint?
var TapMaxDelta = CGFloat(10)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init() {
let texture = SKTexture(imageNamed: "Test.png")
super.init(texture: texture, color: UIColor.clear, size: texture.size())
isUserInteractionEnabled = true
}
// ================================================================================================
// Touch Functions
// ================================================================================================
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
firstPoint = firstTouch.location(in: self)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first, let firstPoint = firstPoint {
let curPoint = firstTouch.location(in: self)
if abs(curPoint.x - firstPoint.x) <= TapMaxDelta && abs(curPoint.y - firstPoint.y) <= TapMaxDelta {
print("tap yo")
}
}
}
}