2010-09-03 60 views
3

我有一个代码,移动对触摸事件的UIImage控制iphone游戏图像的运动

是这样的:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch=[[event allTouches]anyObject]; 
touchOffset= image.center.x-[touch locationInView:touch.view].x; 
} 


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch=[[event allTouches]anyObject]; 
//paddle movemant coordinates 
float distanceMoved=([touch locationInView:touch.view].x+touchOffset)- image.center.x; 
float newX= image.center.x+distanceMoved; 
if(newX>30 &&newX<290) 
image.center=CGPointMake(newX, image.center.y); 
if(newX>290) 
image.center=CGPointMake(290, image.center.y); 
if(newX<30) 
image.center=CGPointMake(30, image.center.y); 
} 

但问题是,我想这个行动必须执行上按钮(左 - 右按钮)动作。当我按下左按钮UIImage稍微移动到左侧。而当我按下右键UIImage移动一点点到右侧基本上我想要图像移动控制器只有在iphone或pad x轴上的按钮。

由于事先

+0

我有点困惑。你有UIImages,你有检测到触摸,你使用这些图像作为按钮,对不对?为什么不使用UIButtons并将图像嵌入到按钮中?这样,无论何时点击按钮,您都可以拦截执行点击的按钮实例。 – 2010-09-06 02:41:40

+0

没有让你dear.help我的朋友 – Rocky 2010-09-07 14:05:14

回答

0

的CGRect帧= [图像帧]。 frame.origin.x + = 10.0f; [image setFrame:frame]; 使用此&映射您的按钮动作。

1
CGRect frame = [image frame]; 
frame.origin.x += 10.0f; 
[image setFrame:frame]; 
+0

我该如何使用这个 – Rocky 2010-09-21 10:58:46

+0

它的工作,但我想在游戏中移动对象从左到右,从右到左帮助游戏手柄。 – Rocky 2010-10-04 13:19:40