Ios 触摸移动背景图像
当前情况 查看加载,加载背景图像 将Ios 触摸移动背景图像,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,当前情况 查看加载,加载背景图像 将十字线布尔值显示为false -(void)didMoveToView:(SKView *)view { /* Setup your scene here */ self.anchorPoint = CGPointMake(0.5, 0.5); self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0]; //Add backg
十字线布尔值显示为false
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
self.anchorPoint = CGPointMake(0.5, 0.5);
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
//Add background
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"backgroundTemp" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
backgroundImage = [SKSpriteNode spriteNodeWithTexture:texture];
backgroundImage.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
backgroundImage.position = CGPointMake(0.5, 0.5);
backgroundImage.zPosition = 0;
[self addChild:backgroundImage];
//END Add Background
removeArray = [[NSMutableArray alloc] init];
reticleShow = false;
}
因此,在加载背景图像后,显示。唯一的其他选项是按下屏幕以放大背景图像
,并加载十字线
图像
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if (reticleShow == false) {
//Add Reticle
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"reticle_001" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
SKSpriteNode *reticle = [SKSpriteNode spriteNodeWithTexture:texture];
reticle.zPosition = 1;
reticle.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
reticle.position = CGPointMake(0.5, 0.5);
[self addChild:reticle];
[removeArray addObject:reticle];
//End Add Reticle
//Enlarge background
backgroundImage.size = CGSizeMake(self.frame.size.width*3, self.frame.size.height*3);
//End Enlarge background
reticleShow = true;
} else {
// //Remove reticle
// backgroundImage.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
// [self removeChildrenInArray:removeArray];
// reticleShow = false;
// //end remove reticle
//enable drag
//end enable drag
}
}
现在,它提供了放大,布尔值可以防止进一步添加精灵,以及提供十字线图像
现在,在这个加载之后。或者已经进行了首次印刷。我现在希望能够拖动背景。i、 e移动背景并使十字线居中
所以我做了这个
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:backgroundImage]) { <--- ERROR HERE sksprite node to type nsstring
CGPoint previousLocation = [touch previousLocationInNode:self];
float diff = location.y - previousLocation.y;
CGPoint newPosition = CGPointMake(node.position.x, node.position.y +diff);
if (newPosition.y > 230) {
newPosition.y = 230;
}
node.position = newPosition;
}
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
用于(UITouch*触摸屏){
CGPoint位置=[触摸位置Innode:self];
SKNode*node=[自身节点点:位置];
if([node.name IsequalString:backgroundImage]){230){
新位置y=230;
}
node.position=newPosition;
}
}
}
三个问题
如何在较小的z索引上拖动图像?i、 拖动背景而不是十字线
放大后如何移动背景图像
如何传递sknode&nsstring错误?backgroundImage.name=@“backgroundImage”
问题一
拖动较小的Z索引
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [backgroundImage nodeAtPoint:location];
if ([node.name isEqualToString:@"backgroundImage"]) {
CGPoint previousLocation = [touch previousLocationInNode:self];
float ydiff = location.y - previousLocation.y;
float xdiff = location.x - previousLocation.x;
CGPoint newPosition = CGPointMake(node.position.x - xdiff, node.position.y - ydiff);
if (newPosition.y > 230) {
newPosition.y = 230;
}
node.position = newPosition;
}
}
}
插入这个
retile.userInteractionEnabled=NO代码>
问题二
放大不会影响它
问题三
为检测命名SkSpriteNode
backgroundImage.name=@“backgroundImage”代码>3-SKNode具有名称属性。你需要设置它。例如,myNode.name=@“myName”;然后使用if([myNode.name isEqualToString:@“myName”])@sangony-Excellent检查它,这回答了我的一个问题。感谢you@sangony现在我可以在z索引0上移动背景,但是,如果十字线z索引1被覆盖,我无法移动背景图像。如果节点的z位置高于背景,则不需要用户交互,例如触摸事件,请将其userInteractionEnabled属性设置为否。