我只是想知道我该怎么做,并希望有人做过类似的事情。当手指留在屏幕上时,我希望精灵可以缩放。我可以查看的任何提示、一般指南或方法将不胜感激。
4 回答
定义您想要执行的操作,在这种情况下...... ScaleBy(您希望它相对于当前比例,而不是确定的比例)。当收到触摸时,RepeatForever这个动作直到触摸结束,然后停止动作。
如果您想实现一个上限以使项目不会增长超过某个点,那么您的 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];
}
当我让你正确时,你想在手指保持在同一个位置而不移动的情况下缩放精灵,这是正确的吗?
我建议使用Layer
以下方法:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
您可以安排一个基于间隔的动作/方法ccTouchesBegan
来增加精灵的比例(例如每秒增加 0.1),然后ccTouchesEnded
取消该操作并将比例设置回原始值(例如 1.0)。
对于调度,请查看该方法(在任何扩展类中找到CocosNode
):
-(无效)时间表:(SEL)s;
嗯......似乎 sprite 类有一个scale方法,可以让您将其比例设置为浮点值,其中 1.0 是默认(“本机”)比例。
你不是很清楚你希望精灵如何缩放,如果它应该缩放以“跟随”触摸,即让用户通过拖动重新调整精灵的大小,或者如果只是要(例如)改变它的比例通知用户触摸已注册。你需要弄清楚这些事情,并计算出你想要的比例。
我刚刚使用 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;
}