Objective c Cocos2d:在触摸精灵时缩放它
我只是想知道我该怎么做,希望有人也做过类似的事情。我想要一个精灵的规模,因为手指仍然在屏幕上。任何我能看到的提示、一般指南或方法都将不胜感激。好吧。。。sprite类似乎有一个方法可以让您将其比例设置为浮点值,其中1.0是默认(“本机”)比例Objective c Cocos2d:在触摸精灵时缩放它,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,我只是想知道我该怎么做,希望有人也做过类似的事情。我想要一个精灵的规模,因为手指仍然在屏幕上。任何我能看到的提示、一般指南或方法都将不胜感激。好吧。。。sprite类似乎有一个方法可以让您将其比例设置为浮点值,其中1.0是默认(“本机”)比例 您不太清楚希望精灵如何缩放,如果它应该缩放以“跟随”触摸,即让用户通过拖动来重新调整精灵的大小,或者如果只是(例如)更改其缩放以通知用户触摸已注册。你需要把这些事情弄清楚,然后计算出你想要的比例。好吧。。。sprite类似乎有一个方法可以让您将其比例设置为
您不太清楚希望精灵如何缩放,如果它应该缩放以“跟随”触摸,即让用户通过拖动来重新调整精灵的大小,或者如果只是(例如)更改其缩放以通知用户触摸已注册。你需要把这些事情弄清楚,然后计算出你想要的比例。好吧。。。sprite类似乎有一个方法可以让您将其比例设置为浮点值,其中1.0是默认(“本机”)比例
您不太清楚希望精灵如何缩放,如果它应该缩放以“跟随”触摸,即让用户通过拖动来重新调整精灵的大小,或者如果只是(例如)更改其缩放以通知用户触摸已注册。你需要弄清楚这些事情,然后计算出你想要的比例。当我把你弄对的时候,你想要在手指保持在同一点上而不移动的情况下缩放精灵,对吗 我建议通过以下方法使用
层
:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
您可以在cctouchesbeated
中计划基于间隔的操作/方法以增加精灵的比例(例如每秒增加0.1),并在cctouchesend
中取消该操作并将比例设置回原始值(例如1.0)
对于调度,请查看该方法(可在任何扩展CocosNode
的类中找到):
-(无效)附表:(SEL)s 如果我没弄错,你想在手指保持在同一点而不移动的情况下缩放精灵,对吗 我建议通过以下方法使用
层
:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
您可以在cctouchesbeated
中计划基于间隔的操作/方法以增加精灵的比例(例如每秒增加0.1),并在cctouchesend
中取消该操作并将比例设置回原始值(例如1.0)
对于调度,请查看该方法(可在任何扩展CocosNode
的类中找到):
-(无效)附表:(SEL)s 定义要执行的操作,在本例中。。。ScaleBy(根据您的需要,相对于当前比例,而不是最终比例)。收到触摸后,重复此操作直到触摸结束,然后停止操作 如果您想要实现一个上限,以便项目不会超过某个点,那么您的ScaleBy将是一个ScaleTo,其上限设置为目标比例。然而,你将不得不玩弄持续时间来获得你想要的感觉。太快、太慢等。。。这些由你决定
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
if(CGRectContainsPoint(particularSpriteRect, location)) {
// scale by 1.25 every 0.25 second while finger touching
id a = [RepeatForever actionWithAction: [ScaleBy actionWithDuration: 0.25 scale: 1.25];
[a setTag: YOUR_TAG];
[particularSprite runAction: a];
return kEventHandled;
}
}
- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
// code to determine correct finger/touch releases omitted
[particularSprite stopActionByTag: YOUR_TAG];
}
定义要执行的操作,在本例中。。。ScaleBy(根据您的需要,相对于当前比例,而不是最终比例)。收到触摸后,重复此操作直到触摸结束,然后停止操作 如果您想要实现一个上限,以便项目不会超过某个点,那么您的ScaleBy将是一个ScaleTo,其上限设置为目标比例。然而,你将不得不玩弄持续时间来获得你想要的感觉。太快、太慢等。。。这些由你决定
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
if(CGRectContainsPoint(particularSpriteRect, location)) {
// scale by 1.25 every 0.25 second while finger touching
id a = [RepeatForever actionWithAction: [ScaleBy actionWithDuration: 0.25 scale: 1.25];
[a setTag: YOUR_TAG];
[particularSprite runAction: a];
return kEventHandled;
}
}
- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
// code to determine correct finger/touch releases omitted
[particularSprite stopActionByTag: YOUR_TAG];
}
我刚刚使用cocos2D和SpaceManager实现了触摸放大/缩小。已经被接受的答案看起来不错,但我想展示一下,如果您已经在使用SpaceManager,您可以如何进行此操作 注意:这个片段可以一次放大和缩小。可以查看已经接受的答案,看看您将如何修改它,使其仅在发布时进行缩小 我就是这样做的:
#pragma mark touch
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"Touched began");
CGPoint position = [touch locationInView: [touch view]];
CGPoint positionLocal =[[CCDirector sharedDirector] convertToGL: position];
cpShape *touchedShape = [smgr getShapeAt:positionLocal];
if (touchedShape != nil) {
NSLog(@"something was touched");
cpCCSprite *cpCCSOwner = (cpCCSprite *) touchedShape->data;
// Let's do 'pulse' special effect on whatever we just touched, just to give the user some feedback
cpFloat originalScale_ = cpCCSOwner.scale;
CCFiniteTimeAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];// zoom in
CCFiniteTimeAction *shrinkAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];// zoom out
CCSequence *actions = [CCSequence actions:zoomAction, shrinkAction, nil];// zoom in, then zoom out
[cpCCSOwner runAction:actions];// now
// Since I just grabbed the object, it should stop moving
cpCCSOwner.shape->body->v = cpvzero;
// Let's keep track of what we are touching.
cpCCSpriteTouched = cpCCSOwner; //<- I just added this to the class, you'll need something more sophisticated from non-trivial cases
} else {
NSLog(@"nothing was actually touched - you missed");
cpCCSpriteTouched = nil;
}
return YES;
}
#pragma标记触摸
-(BOOL)cctouch开始:(UITouch*)触摸事件:(UIEvent*)事件{
NSLog(@“触摸开始”);
CGPoint位置=[触摸位置视图:[触摸视图]];
CGPoint positionLocal=[[CCDirector sharedDirector]convertToGL:position];
cpShape*touchedShape=[smgr getShapeAt:positionLocal];
if(touchedShape!=nil){
NSLog(@“触动了什么”);
cpCCSprite*cpCCSOwner=(cpCCSprite*)触摸形状->数据;
//让我们对刚刚触摸过的任何东西做“脉冲”特效,只是给用户一些反馈
cpFloat originalScale=cpCCSOwner.scale;
CCFiniteTimeAction*zoomAction=[CCScaleToActionWithDuration:0.1f比例:原始比例*1.2f];//放大
CCFiniteTimeAction*shrinkAction=[CCScaleTo actionWithDuration:0.1f scale:originalScale_quo;//缩小
CCSequence*actions=[CCSequence actions:zoomAction,shrinkAction,nil];//放大,然后缩小
[cpCCSOwner runAction:actions];//现在
//因为我刚刚抓住了那个物体,它应该停止移动了
cpCCSOwner.shape->body->v=cpvzero;
//让我们跟踪我们正在接触的内容。
cpccspritetouch=cpCCSOwner;//我刚刚使用cocos2D和SpaceManager实现了触摸放大/缩小。已经接受的答案看起来不错,但我想展示一下,如果您已经在使用SpaceManager,您可以如何实现这一点
注意:这个片段可以一次放大和缩小。可以查看已经被接受的答案,看看您如何修改它,使其仅在发布时进行缩小
我就是这样做的:
#pragma mark touch
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"Touched began");
CGPoint position = [touch locationInView: [touch view]];
CGPoint positionLocal =[[CCDirector sharedDirector] convertToGL: position];
cpShape *touchedShape = [smgr getShapeAt:positionLocal];
if (touchedShape != nil) {
NSLog(@"something was touched");
cpCCSprite *cpCCSOwner = (cpCCSprite *) touchedShape->data;
// Let's do 'pulse' special effect on whatever we just touched, just to give the user some feedback
cpFloat originalScale_ = cpCCSOwner.scale;
CCFiniteTimeAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];// zoom in
CCFiniteTimeAction *shrinkAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];// zoom out
CCSequence *actions = [CCSequence actions:zoomAction, shrinkAction, nil];// zoom in, then zoom out
[cpCCSOwner runAction:actions];// now
// Since I just grabbed the object, it should stop moving
cpCCSOwner.shape->body->v = cpvzero;
// Let's keep track of what we are touching.
cpCCSpriteTouched = cpCCSOwner; //<- I just added this to the class, you'll need something more sophisticated from non-trivial cases
} else {
NSLog(@"nothing was actually touched - you missed");
cpCCSpriteTouched = nil;
}
return YES;
}
#pragma标记触摸
-(BOOL)cctouch开始:(UITouch*)触摸事件:(UIEvent*)事件{
NSLog(@“触摸开始”);
CGPoint位置=[触摸位置视图:[触摸视图]];
CGPoint positionLocal=[[CCDirector sharedDirector]convertToGL:position];
cpShape*触摸形状=