如何在一个位置随机选择一个节点?

时间:2016-02-01 01:29:57

标签: swift sprite-kit

我有五个不同颜色的圆圈,每个圆圈都在相同的功能中调用,但具有不同的物理实体和名称。我希望他们都能从屏幕底部向上移动,屏幕上只有一个节点,一次从一个设置位置(即CGPointMake(100,-500))就像生成一个随机颜色的节点后,4秒会出现一个不同的随机选择的?

有没有办法让所有人都被单独识别并执行此操作?

代码:

      func circles() {

     let sprites = SKSpriteNode(imageNamed: "Circle1")
       sprites.position = CGPointMake(45, -200)

        sprites.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        sprites.physicsBody?.dynamic = true
        sprites.physicsBody?.affectedByGravity = false
        sprites.physicsBody?.categoryBitMask = BodyType.player.rawValue
        sprites.physicsBody?.contactTestBitMask = BodyType.enemy.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue | BodyType.five.rawValue

        sprites.size = CGSizeMake(45, 45)
        all.addChild(sprites)
        let sprite2 = SKSpriteNode(imageNamed: "Circle2")
        sprite2.position = CGPointMake(60, -750)
        sprite2.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        sprite2.physicsBody?.categoryBitMask = BodyType.eight.rawValue
        sprite2.physicsBody?.contactTestBitMask = BodyType.nine.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
        sprite2.physicsBody?.dynamic = true
        sprite2.physicsBody?.affectedByGravity = false

        sprite2.size = CGSizeMake(45, 45)
         addChild(sprite2)
        let sprite3 = SKSpriteNode(imageNamed: "circle3")
        sprite3.position = CGPointMake(100, -1200)
        sprite3.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        sprite3.physicsBody?.categoryBitMask = BodyType.six.rawValue
        sprite3.physicsBody?.contactTestBitMask = BodyType.seven.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.nine.rawValue
        sprite3.physicsBody?.dynamic = true
        sprite3.physicsBody?.affectedByGravity = false

        sprite3.size = CGSizeMake(45, 45)
        addChild(sprite3)
        let sprite4 = SKSpriteNode(imageNamed: "Circle4")
        sprite4.position = CGPointMake(150, -1400)
        sprite4.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        sprite4.physicsBody?.categoryBitMask = BodyType.ground.rawValue
        sprite4.physicsBody?.contactTestBitMask = BodyType.other.rawValue | BodyType.player.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
        sprite4.physicsBody?.dynamic = true
        sprite4.physicsBody?.affectedByGravity = false

        sprite4.size = CGSizeMake(45, 45)
        addChild(sprite4)
        let sprite5 = SKSpriteNode(imageNamed: "Circle5")
        sprite5.position = CGPointMake(200, -850)
        sprite5.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        sprite5.physicsBody?.categoryBitMask = BodyType.ya.rawValue
        sprite5.physicsBody?.contactTestBitMask = BodyType.five.rawValue | BodyType.player.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue


        sprite5.physicsBody?.dynamic = true
        sprite5.physicsBody?.affectedByGravity = false
        sprite5.size = CGSizeMake(45, 45)
        addChild(sprite5)
          }

非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用public class PodcastListFragment extends Fragment { private View view; private SwipeRefreshLayout refreshLayout; private ListView listView; private int channelId; private ArrayAdapter adapter; private ArrayList<Podcast>podcastList = new ArrayList<Podcast>() @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.podcast_list_fragment,container,false); adapter = new PodcastListAdapter(getActivity(),podcastList); listView = (ListView)view.findViewById(R.id.listview_podcast_list); listView.setAdapter(adapter); TextView textView = (TextView)view.findViewById(R.id.list_title); textView.setText(getArguments().getString("title")); this.channelId = getArguments().getInt("channel"); updateListItems(); return view; } private void updateListItems() { String url = getResources().getString(R.string.api_endpoint)+"podcast"; if(channelId>0) { url = url + "?channel="+String.valueOf(channelId); } JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try{ String status = response.getString("status"); if(status.equals(getResources().getString(R.string.success))) { JSONArray items = response.getJSONArray("items"); ArrayList<Podcast> podcasts = new ArrayList<Podcast>(); if(items.length()>0) { for(int i=0;i<items.length();i++) { Podcast podcast = Podcast.fromJsonObject(items.getJSONObject(i)); podcasts.add(podcast); } } podcastList.addAll(podcasts); adapter.notifyDataSetChanged(); } } catch (JSONException e) { Toast.makeText(getActivity().getBaseContext(),"Error encountered in transferring data from server",Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity().getBaseContext(),"Error encountered in transferring data from server",Toast.LENGTH_SHORT).show(); } }); MySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest); } } 的名称属性。

来自documentation

  

用于为节点指定名称以供以后参考。选择时   对于节点的名称,您应该确定每个节点是否都是唯一的   名称或某些节点是否共享公用名。如果你给   节点一个唯一的名称,您可以稍后通过调用找到该节点   childNodeWithName:方法。如果名称由多个节点共享,则   name通常意味着这些都是你的类似对象类型   游戏。在这种情况下,您可以通过调用来迭代这些对象   enumerateChildNodesWithName:usingBlock:method。