簡體   English   中英

AS3 hitTestObject無法正常工作

[英]AS3 hitTestObject is not working

我制作的Flash可以在鍵盤上移動一個圓圈,並且可以將閃光燈擊中牆。 但是方法'hitTestObject'無法正常工作...

侯要解決這個問題嗎?

import flash.events.KeyboardEvent;
import flash.events.Event;

stage.addEventListener(KeyboardEvent.KEY_DOWN,atKeydown);
stage.addEventListener(KeyboardEvent.KEY_UP,atKeyup);

function atKeydown(e :KeyboardEvent) :void
{
    if(e.keyCode == 38) stage.addEventListener(Event.ENTER_FRAME,enterYUp);
    if(e.keyCode == 40) stage.addEventListener(Event.ENTER_FRAME,enterYDown);
    if(e.keyCode == 37) stage.addEventListener(Event.ENTER_FRAME,enterXLeft);
    if(e.keyCode == 39) stage.addEventListener(Event.ENTER_FRAME,enterXRight);
}

function atKeyup(e :KeyboardEvent) :void
{
    if(e.keyCode == 38) stage.removeEventListener(Event.ENTER_FRAME,enterYUp);
    if(e.keyCode == 40) stage.removeEventListener(Event.ENTER_FRAME,enterYDown);
    if(e.keyCode == 37) stage.removeEventListener(Event.ENTER_FRAME,enterXLeft);
    if(e.keyCode == 39) stage.removeEventListener(Event.ENTER_FRAME,enterXRight);
}

function enterYUp(e :Event) :void
{
    ball.y -= 10;
}
function enterYDown(e :Event) :void
{
    ball.y += 10;
}
function enterXLeft(e :Event) :void
{
    ball.x -= 10;
}
function enterXRight(e :Event) :void
{
    ball.x += 10;
}

if(ball.hitTestObject(wall) == true) trace("!!!!");
function enterYUp(e :Event) :void
{
 var bool = checkHit("y", -10); // each frame checks for a hit, returns a Boolean (see the function below)
 carryOut(bool, "y", -10); // the movement is carried out based on the Boolean
}
function enterYDown(e :Event) :void
{
var bool = checkHit("y", +10); // you can send the "y" property, and where it wants to go.
 carryOut(bool, "y", +10);
}
function enterXLeft(e :Event) :void
{
var bool = checkHit("x", -10);
 carryOut(bool, "x", -10);
}
function enterXRight(e :Event) :void
{
 var bool = checkHit("x", +10);
 carryOut(bool, "x", -10);
}

function checkHit(_prop, _num):Boolean
{ 
 // This single function will run every frame, comparing the balls 'predicted position' 
  if((ball[_prop] + _num) <=  _numCheck)
  {
  // i might use hitTestPoint instead, but I hate using hitTestObject... it can act weird sometimes.
  return true;
  }
  else
  {
  return false;
  }
}


function carryOut(bool, _prop,_num):void
{
  if(bool)
 {
  trace("hit");
 }
 else
 {
  // if the Boolean returned false
  // ball[_prop] is ball.x or ball.y depending on if you sent "x" or "y".
  // it will ADD the _num you sent, so adding -10 is the same as subtracting.
  ball[_prop] += _num;
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM