在cocos2d iphone中创建透明按钮

时间:2012-02-07 12:35:07

标签: iphone button cocos2d-iphone

我正在开发一款cocos2d游戏。在那场比赛中,我有一个角色,并且根据角色的触摸位置应该触发不同的动作,我如何在我的cocos2d游戏中实现这一点。 有没有什么方法可以在cocos2d中实现透明按钮。

提前致谢

2 个答案:

答案 0 :(得分:3)

创建CCMenuItemSprite(按钮)时,为其指定一个用于显示的精灵。

然后,您可以通过更改精灵的不透明度属性或使其完全不可见来更改按钮的外观。

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;

答案 1 :(得分:0)

我不完全确定我理解这个问题,更多信息会有所帮助,但我会尽力回答。

假设你有一个角色类我会实现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;
}

希望这会有所帮助!