如何检测spritekit中的触摸?

时间:2016-07-03 20:54:28

标签: xcode swift sprite-kit touch

我有一个精灵,如果按下精灵,我想打印一些文字。 我在其上找到的每个教程似乎都已过时。 这怎么可能?

它应该是这样的:

$ whois -h whois.name.com stackoverflow.com

Domain Name: STACKOVERFLOW.COM 
Registry Domain ID: 108907621_DOMAIN_COM-VRSN 
Registrar WHOIS Server: whois.name.com 
Registrar URL: http://www.name.com 
Updated Date: 2016-06-08T04:23:11Z 
Creation Date: 2003-12-26T19:18:07Z 
Registrar Registration Expiration Date: 2016-12-26T19:18:07Z 
Registrar: Name.com, Inc. 
Registrar IANA ID: 625 
Reseller:  
Domain Status: clientTransferProhibited 
Registry Registrant ID:  
Registrant Name: Sysadmin Team 
Registrant Organization: Stack Exchange, Inc. 
Registrant Street: 110 William St , Floor 28 
Registrant City: New York 
Registrant State/Province: NY 
Registrant Postal Code: 10038 
Registrant Country: US 
Registrant Phone: +1.2122328280 
Registrant Email: sysadmin-team@stackoverflow.com

[...]

2 个答案:

答案 0 :(得分:5)

如果您的Sprite有自定义类,则只需覆盖touchesBegan方法

实施例

class Player: SKSpriteNode {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        userInteractionEnabled = true
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("Did touch Player sprite")
    }
}
  

不要忘记设置userInteractionEnabled = true

答案 1 :(得分:1)

在touchesBegan函数或touchesEnded中,您可以添加此代码

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="input-field">
          <%= f.label :email %>
          <%= f.email_field :email, autofocus: true %>
        </div>

        <div class="input-field">
          <%= f.label :password %>
          <%= f.password_field :password, autocomplete: "off" %>
        </div>

...