获取特定区域的节点列表? [英] Get a list of nodes in an specific area?

查看:21
本文介绍了获取特定区域的节点列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个横向游戏,我需要知道某个区域中有哪些节点才能实现视线"之类的东西.现在我正在尝试使用 enumerateBodyiesInRect() 但是它检测到的物体距离评估的矩形 20px 或更多,我无法弄清楚为什么它如此不精确.

I'm working in a side-scolling game and I need to know what nodes are in an area to implement something like "line of sight". Right now I'm trying using enumerateBodyiesInRect() however it's detecting bodies that are 20px or more from the evaluated rect and I cannot figure out why it's so imprecise.

这就是我现在正在尝试的:

This is what I'm trying now:

import SpriteKit
import CoreMotion

class GameScene: SKScene, SKPhysicsContactDelegate
{
var player = SKShapeNode()
var world = SKShapeNode()
var rShape = SKShapeNode()

override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
    self.scaleMode = SKSceneScaleMode.AspectFit
    self.size = view.bounds.size

    // Add world
    world = SKShapeNode(rectOfSize: view.bounds.size)
    world.physicsBody = SKPhysicsBody(edgeLoopFromPath: world.path)
    world.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2) // Move camera
    self.addChild(world)

    // Add player
    player = SKShapeNode(rectOfSize: CGSize(width: 25, height: 25))
    player.physicsBody = SKPhysicsBody(rectangleOfSize: player.frame.size)
    player.physicsBody.dynamic = false
    player.strokeColor = SKColor.blueColor()
    player.fillColor = SKColor.blueColor()
    player.position = CGPointMake(90, -50)
    world.addChild(player)
    }


override func update(currentTime: CFTimeInterval) {
    // Define rect position and size (area that will be evaluated for bodies)
    var r : CGRect = CGRect(x: 200, y: 200, width: 25, height: 25)

    // Show rect for debug
    rShape.removeFromParent()
    rShape = SKShapeNode(rect: r)
    rShape.strokeColor = SKColor.redColor()
    self.addChild(rShape)

    // Evaluate rect
    rShape.fillColor = SKColor.clearColor()
    self.physicsWorld.enumerateBodiesInRect(r) {
        (body: SKPhysicsBody!, stop: UnsafePointer<ObjCBool>) in
         self.rShape.fillColor = SKColor.redColor() // Paint the area blue if it detects a node
         }
    }
}

此代码应在屏幕上显示评估的 rect 和 ray(用于调试目的),如果它们与播放器节点接触,则将它们涂成红色.但是,您可以在屏幕截图中看到,当玩家距离它 25 像素或更多时,它是如何变成红色的,这就像绘图有点偏离,或者比正在评估的实际区域小.您可以将其复制粘贴到项目中以复制问题.

This code should show the evaluated rect and ray on the screen (for debugging purposes) and paint them red if they contact the player node. However you can see in the screenshot how it turns red when the player is 25px or more away from it, it's like if the drawing is a little bit off, or smaller than the actual area being evaluated. You can copy paste it to a project to duplicate the problem.

这可能是因为这只是测试版还是我做错了什么?

Could this be because this is just beta or am I doing something wrong?

推荐答案

您正在创建一个物理世界,其中有一个具有特殊属性"的特定矩形 - 这是您在 enumerateBodiesInRect() 中使用的矩形.为什么不创建一个具有所需矩形尺寸的不可见、惰性物理体,然后使用 SKPhysicsBody 来检查碰撞和/或接触?然后,您可以使用 allContactedBodies() 或一些委托回调来了解您的特殊矩形内的其他主体.

You are creating a physical world where there is a specific rectangle that has 'special properties' - this is the rectangle that you use in enumerateBodiesInRect(). Why not create an invisible, inert physical body with the required rectangular dimension and then use SKPhysicsBody to check for collisions and/or contacts? You could then use allContactedBodies() or some delegate callbacks to learn what other bodies are inside your special rectangle.

把它想象成牵引梁"或扭曲矩形".

Think of it like a 'tractor beam' or a 'warp rectangle'.

这篇关于获取特定区域的节点列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆