gpt4 book ai didi

ios - 触摸时更改图像

转载 作者:行者123 更新时间:2023-11-29 03:02:25 24 4
gpt4 key购买 nike

我需要一个图像,当它被触摸时会自行改变。

目前改变的图像是下一个产生的图像而不是自己。

@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

就像我之前说的,我需要自己的 Sprite 来改变纹理,而不是“下一个生成的 Sprite ”。

如果您能帮助解决这个问题,我们将不胜感激,谢谢。

最佳答案

您不想使用 touchesBegan - 您最好使用点击手势识别器。下面我有 _testView,它是我创建并添加到 viewDidLoad 中的 View 的实例变量。然后我创建了一个点击手势识别器,当点击 View 时调用一个函数,并且该函数改变 View 的颜色 - 但在你的情况下你可以调用你的函数来改变图像:

- (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];
}

这首先导致:

enter image description here

然后当我点击 View 时:

enter image description here

关于ios - 触摸时更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161983/

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