帮助我..我正在为我的学校项目制作RPG,而NPC01类中的代码hitTestObject无效。
这是我的代码:
Game.as
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Game extends MovieClip{
var map:Map = new Map();
var player:Player = new Player();
var npc:NPC = new NPC();
public function Game(){
addChild(map);
addChild(npc);
addChild(player);
this.addEventListener(Event.ENTER_FRAME,loop);
}
private function loop():void {
npc.x = map.x;
npc.y = map.y;
}
}
}
NPC.as
package {
import flash.display.MovieClip;
import flash.events.Event;
public class NPC extends MovieClip {
var npc01:NPC01 = new NPC01();
public function NPC() {
// constructor code
addChild(npc01);
npc01.x = 20;
npc01.y = 0;
}
}
}
NPC01.as
package {
import flash.display.MovieClip;
import flash.events.Event;
public class NPC01 extends MovieClip {
var player:Player = new Player();
public function NPC01() {
// constructor code
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
if(player.hitTestObject(npc)) { //npc is a movieclip inside NPC01
trace("HIT!!");
}
}
我没有收到任何错误消息。只是,每当播放器movieclip点击npc movieclip时,输出都不显示..
我还尝试使用以下方式追踪他们的位置:
trace( player.getBounds(stage) );
trace( npc.getBounds(stage) );
结果:
(x=343, y=218, w=26, h=45)
(x=334, y=218, w=32, h=51)
似乎他们正在相撞。
但是当我在函数循环下的Game.as中编写hitTestObject代码时,当玩家点击npc时跟踪输出显示为up。
我没有任何问题移动特征'玩家'。
抱歉我的英语不好。
我也在循环函数下的NPC01.as中尝试了这些:
if (npc.hitTestObject(player)) {
trace("Hit01");
}
if (npc.hitTestObject(player)) {
trace("Hit02");
}
if (npc.hitTestPoint(player.x, player.y, true)) {
trace("Hit03");
}
if (player.hitTestPoint(npc.x, npc.y, true)) {
trace("Hit04");
}
但只有“Hit03”正在回应..
答案 0 :(得分:1)
以下是您拥有的DisplayObject:
请注意,您已经创建了两个玩家(一个在游戏中,另一个在NPC01中)。你确定你正在测试正确的吗?
我猜你可能只想要一个。试试这个。
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Game extends MovieClip{
var map:Map = new Map();
var player:Player = new Player();
var npc:NPC = new NPC();
public function Game(){
addChild(map);
addChild(npc);
addChild(player);
this.addEventListener(Event.ENTER_FRAME,loop);
}
private function loop():void {
npc.x = map.x;
npc.y = map.y;
if(player.hitTestObject(npc)) {
trace("HIT!!");
}
}
}
从NPC01中删除播放器:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class NPC01 extends MovieClip {
public function NPC01() {
}
}
答案 1 :(得分:1)
问题是,当您只需一个时,您正在创建两个不同的播放器对象。
这应该更适合您的目的:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Game extends MovieClip{
var map:Map = new Map();
var player:Player = new Player();
var npc:NPC = new NPC(player);
public function Game(){
addChild(map);
addChild(npc);
addChild(player);
this.addEventListener(Event.ENTER_FRAME,loop);
}
private function loop():void {
npc.x = map.x;
npc.y = map.y;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
public class NPC extends MovieClip {
var player:Player;
var npc01:NPC01;
public function NPC(player:Player) {
// constructor code
this.player = player;
npc01 = new NPC01(player);
addChild(npc01);
npc01.x = 20;
npc01.y = 0;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
public class NPC01 extends MovieClip {
var player:Player;
public function NPC01(player:Player) {
// constructor code
this.player = player;
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
if(player.hitTestObject(npc)) { //npc is a movieclip inside NPC01
trace("HIT!!");
}
}