这完全在actionscript 3.0中,并且是flash cs5.5中的代码 我做了一个推/拉游戏,其中一个玩家推动一个块。级别最终将非常大并且每个类具有多个实例,因此在舞台上用实例名称命名每个符号是不切实际的。 我有这个engine.as文件我写了,但我无法弄清楚为什么它不会工作。继承了整个班级,但我试图使用的是hitTestClass函数; 包 {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
public class Engine extends MovieClip
{
private static var _instance:MovieClip;
public function Engine()
{
stop();
_instance = this;
}
public static function add(object:Object):void
{
if (object!=DisplayObject) return;
_instance.addChild(object as DisplayObject);
}
public static function remove(object:Object):void
{
if (object is Array) {
removeList(object as Array);
return;
}
if (!(object is DisplayObject) || object.parent!=_instance) return;
_instance.removeChild(object as DisplayObject);
}
public static function removeList(objects:Array):void
{
while (objects.length>0) remove(objects.pop());
//trace(objects.length);
}
public static function getByName(name:String):Object {
if (_instance) {
for (var i:int = 0; i < _instance.numChildren; i++)
{
if (_instance.getChildAt(i).name==name) {
return _instance.getChildAt(i);
}
}
}
return null;
}
public static function getAllByName(name:String):Array {
var list:Array = new Array();
if (_instance) {
for (var i:int = 0; i < _instance.numChildren; i++)
{
if (_instance.getChildAt(i).name==name) {
list.push(_instance.getChildAt(i));
}
}
}
return list;
}
public static function getByClass(className:Class):Object {
if (_instance) {
for (var i:int = 0; i < _instance.numChildren; i++)
{
if (_instance.getChildAt(i) is className) {
return _instance.getChildAt(i);
}
}
}
return null;
}
public static function getAllByClass(className:Class):Array {
var list:Array = new Array();
if (_instance) {
for (var i:int = 0; i < _instance.numChildren; i++)
{
if (_instance.getChildAt(i) is className) {
list.push(_instance.getChildAt(i));
}
}
}
return list;
}
public static function hitTestName(object:DisplayObject, name:String):Object
{
var list:Array = getAllByName(name);
return hitTestList(object, list);
}
public static function hitTestClass(object:DisplayObject, className:Class):Object
{
var list:Array = getAllByClass(className);
return hitTestList(object, list);
}
public static function hitTestList(object:DisplayObject, list:Array):Object
{
var hits:Array = new Array();
for (var i:int=0; i<list.length; i++) {
if (object != list[i] && object.hitTestObject(list[i])) {
hits.push(list[i]);
}
}
return hits;
}
}
}
在我的游戏中,我使用每个&#34; block&#34;类中的更新函数,这里是他们检查命中测试的地方。这是wallBlock类中的一个示例,它确保char可以通过wallBlock运行。 function ableToCheckSides() {
if ((this).x-_root.mcChar.x<63.5 && (this).x-_root.mcChar.x>-63.5)
{
//trace("booty");
//ableToCheckXSides=false;
if((this).y-_root.mcChar.y >=-63 && (this).y-_root.mcChar.y<0 && ableToCheckXSides==false)
{
//ableToCheckXSides=false;
//_root.mcChar.x=_root.mcChar.x;
if(hitTestObject(_root.mcChar)==true)
{
_root.mcChar.y=(this).y+63.5;
}
//if char is moving down and hits the top side of tile
}
else if((this).y-_root.mcChar.y<=63 && (this).y-_root.mcChar.y>0 && ableToCheckXSides==false)
{
//ableToCheckXSides=false;
//_root.mcChar.x=_root.mcChar.x;
if(hitTestObject(_root.mcChar)==true)
{
_root.mcChar.y=(this).y-63.5;
}
//if char is moving up and hits the bottom side of tile
}
}
if ((this).y-_root.mcChar.y<63.5 && (this).y-_root.mcChar.y>-63.5)
{
//trace("YYYYYY");
ableToCheckXSides=true;
if((this).x-_root.mcChar.x >=-63 && (this).x-_root.mcChar.x<0)
{
//trace("inside function");
//ableToCheckYSides=false;
if(hitTestObject(_root.mcChar)==true)
{
_root.mcChar.x=(this).x+63.5;
}
//if char is moving left and hits the right side of tile
}
else if((this).x-_root.mcChar.x<=63 && (this).x-_root.mcChar.x>0)
{
//ableToCheckYSides=false;
if(hitTestObject(_root.mcChar)==true)
{
_root.mcChar.x=(this).x-63.5;
}
//if char is moving right and hits the left side of tile
}
}
else
{
ableToCheckXSides=false;
}
这是问题发生的地方,在同一个&#34;更新&#34;我试图使用我的hitTestClass函数来查看crateBlock是否命中wallBLock,但它永远不会返回true的函数。(如果我将if(hits.length&gt; 0)更改为if(hits)它总是返回true 。)
var hits = Engine.hitTestClass(this, crateBlock);
if (hits.length>0)
{
Engine.remove(hits);
trace("hitting crate");
}
答案 0 :(得分:0)
你不应该使用HitTestObject,你应该使用CollisionTestKit,它是Google代码中提供的库:
https://code.google.com/p/collisiondetectionkit/
HitTestObject本身不能很好地工作。这个库可以让你做更复杂的东西。