首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在cocos2d iphone中创建透明按钮

在cocos2d iphone中创建透明按钮
EN

Stack Overflow用户
提问于 2012-02-07 20:35:07
回答 2查看 1.8K关注 0票数 0

我正在开发一个cocos2d游戏。在游戏中,我有一个角色,根据角色触摸位置,应该触发不同的动作,我如何在我的cocos2d游戏中实现这一点。在cocos2d中有没有实现透明按钮的方法?

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2012-02-07 21:52:31

当您创建一个CCMenuItemSprite (一个按钮)时,您为它指定了一个用于显示的sprite。

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

代码语言:javascript
复制
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;
票数 3
EN

Stack Overflow用户

发布于 2012-02-08 02:19:20

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

假设您有一个character类,我将实现checkTouchesBegan并执行如下操作:

代码语言:javascript
复制
-(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;
}

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9176372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档