我需要一张图像在触摸时自行更改。
目前,变化的图像是下一个产生而不是自我的图像。
@implementation MyScene2
{
Marsugo *marsugo;
SKAction *actionMoveDown;
SKAction *actionMoveEnded;
SKTexture *rescued;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
// Initializes Background
self.currentBackground = [Background generateNewBackground];
[self addChild:self.currentBackground];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
for (SKNode *node in nodes) {
if ([node.name isEqualToString:playerObject]) {
rescued = [SKTexture textureWithImageNamed:@"rescued"];
marsugo.texture = rescued;
// this is changing the image of the next marsugo that spawns instead of self.
}
}
}
}
-(void)addMarsugos
{
// the marsugo is being initialized inside this method, that might be the issue i believe
// Create sprite
marsugo = [[Marsugo alloc]init];
marsugo.xScale = 0.3;
marsugo.yScale = 0.3;
marsugo.zPosition = 75;
// Bounds + Spawn Positions
int minX = marsugo.size.width;
int maxX = self.frame.size.width - marsugo.size.width;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
marsugo.position = CGPointMake(actualX, self.frame.size.height + 50);
[self addChild:marsugo];
// Spawn Timer
int minDuration = 1;
int maxDuration = 10;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Movement Actions
actionMoveDown = [SKAction moveTo:CGPointMake(actualX, CGRectGetMinY(self.frame)) duration:actualDuration];
actionMoveEnded = [SKAction removeFromParent];
[marsugo runAction:[SKAction sequence:@[actionMoveDown, actionMoveEnded]]];
NSLog(@"Marsugo X: %f - Speed: %i", marsugo.position.x, actualDuration);
}
@end
就像我之前说过的,我需要自我精灵来改变纹理,而不是“下一个产卵精灵”。
任何帮助解决此问题将不胜感激,谢谢。
答案 0 :(得分:1)
您不想使用touchesBegan - 您最好使用点击手势识别器。下面我有_testView,它是我创建的一个实例变量,并添加到viewDidLoad中的视图中。然后我创建了一个轻敲手势识别器,在点击视图时调用一个函数,该函数会改变视图的颜色 - 但在您的情况下,您可以调用更改图像的函数:
- (void)viewDidLoad {
[super viewDidLoad];
// create the test view and add it as a subview
_testView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)];
_testView.backgroundColor = [UIColor redColor];
[self.view addSubview:_testView];
// create the tap gesture recognizer and add it to the test view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor)];
[_testView addGestureRecognizer:tapGestureRecognizer];
}
- (void)changeColor {
// here I'm changing the color, but you can do whatever you need once the tap is recognized
_testView.backgroundColor = [UIColor blueColor];
}
这首先导致了这一点:
然后当我点击视图时:
答案 1 :(得分:0)
您可以覆盖-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
课程中的Marsugo
并更改其中的纹理。这样,您就不必检查触摸是否在您的节点内,因为如果不是,则不会调用该方法。