我正在开发一个cocos2d游戏。在游戏中,我有一个角色,根据角色触摸位置,应该触发不同的动作,我如何在我的cocos2d游戏中实现这一点。在cocos2d中有没有实现透明按钮的方法?
提前感谢
发布于 2012-02-07 21:52:31
当您创建一个CCMenuItemSprite (一个按钮)时,您为它指定了一个用于显示的sprite。
然后,可以通过更改精灵的不透明度属性或使其完全不可见来更改按钮的外观。
CCSprite *buttonSpr = [CCSprite spriteWithSpriteFrameName:@"spr.png"];
CCMenuItem *button = [CCMenuItemSprite itemFromNormalSprite:buttonSpr selectedSprite:buttonSpr target:self selector:@selector(buttonTapped:)];
//opacity
buttonSpr.opacity = 50;
//invisible
buttonSpr.visible = false;发布于 2012-02-08 02:19:20
我不完全确定我是否理解了这个问题,更多的信息是否会有所帮助,但我会尽我所能回答这个问题。
假设您有一个character类,我将实现checkTouchesBegan并执行如下操作:
-(BOOL) checkTouchesBegan: (CGPoint*) location
{
//conver the touch coordinates to fit your system
int converty = location->y-160;
int convertx = location->x-240;
//determine where the touch is in relation to the center of the character
float ydif = (1.0)*(converty - character_y);
float xdif = (1.0)*(convertx - character_x);
//determine the angle of the touch
float degrees = atan2f(xdif, ydif) * 57;
//determine the distance between the character and the touch
float squared = xdif*xdif + ydif*ydif;
//if the touch is above the character and within a certain distance
if(degrees >= 45 && degrees < 135 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is below the character and within a certain distance
else if(degrees < -45 && degrees >= -135 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is to the right of the character and within a certain distance
else if(degrees >= -45 && degrees < 45 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is to the left of the character and within a certain distance
else if(sqrt(squared) < 100)
{
doSomething;
return YES;
}
return NO;
}希望这能有所帮助!
https://stackoverflow.com/questions/9176372
复制相似问题