我正在尝试构建一个简单的 iOS 游戏。当我的主菜单加载并出现在屏幕上时,我想让按钮从两侧移入。我唯一的想法是将一些 SKActions 应用于 SKSpriteNodes。但是我必须创建自己的按钮类。有没有更简单的方法来完成这个? cocos2d 中是否已经有像 CCMenuItem/CCMenu 这样的类?或者有没有办法使用 UIButtons?
谢谢
最佳答案
我创建了一个按钮类,它有一个 SKSpriteNode 属性、一个 CGRect 属性和一个 Radius 属性(用于圆形按钮)。当您使用图像初始化按钮时,它会根据图像大小设置 CGRect 和 Radius 属性。然后,您可以通过调用返回 CGRect 的 -collisionRect 方法来检查玩家是否按下按钮,并检查“触摸”位置是否在该矩形内。这样按钮可以四处移动并仍然接收输入。这是代码...
////////////////////头文件////////////////////
//
// ZSButton.h
// PEGO
//
// Created by Zion Siton on 07/09/2013.
// Copyright (c) 2013 zillustrator. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
@interface ZSButton : SKNode {
SKSpriteNode *mySprite;
CGRect myRect;
CGFloat myRadius;
}
@property (nonatomic, strong)SKSpriteNode *mySprite;
@property (nonatomic, assign)CGRect myRect;
@property (nonatomic, assign)CGFloat myRadius;
- (id)initWithTexture:(SKTexture *)texture;
- (BOOL)didRadialCollideWith:(CGPoint)aPoint;
@end
//////////////////实现文件//////////////////////
//
// ZSButtonA.m
// PEGO
//
// Created by Zion Siton on 07/09/2013.
// Copyright (c) 2013 zillustrator. All rights reserved.
//
#import "ZSButton.h"
@implementation ZSButton
@synthesize mySprite, myRect, myRadius;
- (id)init {
// Use a default button image
return [self initWithTexture:[SKTexture textureWithImageNamed:@"ButtonDefault.png"]];
}
- (id)initWithTexture:(SKTexture *)texture {
self = [super init];
if (self) {
// Assumes there is a texture
mySprite = [SKSpriteNode spriteNodeWithTexture:texture];
[self addChild:mySprite];
// Assumes the collision area is the size of the texture frame
myRect = [mySprite frame];
// Assumes the texture is square
myRadius = myRect.size.width * 0.5;
}
return self;
}
- (CGRect)myRect {
CGPoint pos = [self position];
CGFloat originX = pos.x - (mySprite.frame.width * 0.5);
CGFloat originY = pos.y - (mySprite.frame.height * 0.5);
CGFloat width = mySprite.frame.width;
CGFloat height = mySprite.frame.height;
myRect = CGRectMake(originX, originY, width, height);
return myRect;
}
- (BOOL)didRadialCollideWith:(CGPoint)aPoint {
// Get the distance between the button centre and the touch
// ZSMath is a class I wrote that mainly performs trigonometric functions
CGFloat distance = [ZSMath distanceFromA:[self position] toB:aPoint];
// Perform a radial collision check
if (distance < [self myRadius]) {
// HIT!!!
return YES;
}
// MISS!!!
return NO;
}
@end
/////////////////初始化按钮...
// Initialize BACK BUTTON
backButton = [[ZSButton alloc] initWithTexture:
[[ZSGraphicLoader sharedGraphicLoader] frameByName:kBACK_BUTTON]];
[self addChild:backButton];
/////////////////测试碰撞/用户按下
for (UITouch *touch in touches) {
// Get the user's touch location
CGPoint location = [touch locationInNode:self];
// If the user taps on BACK Button Rectangle
if (CGRectContainsPoint(backButton.myRect, location))
{
// DO A RADIAL CHECK
if ([backButton didRadialCollideWith:location])
{
// DO BACK BUTTON STUFF
}
}
}
关于iOS 7 - SpriteKit 按钮/菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913673/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub
我在ruby表单中有一个提交按钮f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id"我想在不使用任何javascript的情况下通过ruby禁用此按钮 最佳答案 添加disabled:true选项。f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id",disabled:true 关于ruby-on-rails-如何在Rails中添加禁用的提交按钮,我们在St
我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac
当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#
正如标题所暗示的那样,我只是在寻找一种无需单击即可按下Shoes中的按钮的方法。我已经搜索了论坛,但不幸的是找不到任何东西。谢谢 最佳答案 这在Windows下的红色鞋子中不起作用,因为Windows控件窃取了所有事件。不过,我设法在window下穿着绿鞋工作。举个例子['green_shoes'].each(&method(:require))Shoes.app{e=edit_linebutton("Clickme!"){alert("Youclickedme.")}keypress{|k|alert("YoupressedEnt