我即将完成我的大学项目。但我坚持使用hittestobject
。
var Player: gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e: MouseEvent): void
{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event: Event): void
{
var Bullet: bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
Bullet.x = Player.x;
Bullet.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(Bullet);
/* addChild(explosion);*/
Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}
function release_shoot(event: Event): void
{
var explosion: explo1 = new explo1();
Player.rotationX = -5;
Player.rotationY = -5;
}
function moveBullet(e: Event): void
{
e.target.y -= 12;
e.target.x -= 96;
if (e.target.y <= -200 || e.target.x <= -200)
{
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event: Event): void
{
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event: Event): void
{
var bullet: MovieClip = MovieClip(event.target);
if (bullet.hitTestObject(mc_target))
{
mc_burst.x = mc_target.x;
mc_burst.y = mc_target.y;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550)
bullet.removeEventListener(Event.ENTER_FRAME, targeting);
else
bullet.y -= 12;
bullet.x -= 96;
}
子弹毫无疑问地进入了目标,我看到了哈哈......但不会用mc_target
取代mc_burst
。
修改
这是我用过的任何人感兴趣的工作代码:
var Player:gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e:MouseEvent):void{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event:Event):void{
var bullet1:bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
bullet1.x = Player.x;
bullet1.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(bullet1);
/* addChild(explosion);*/
bullet1.addEventListener(Event.ENTER_FRAME, targeting);
}
function release_shoot(event:Event):void{
var explosion:explo1 = new explo1();
Player.rotationX =- 5;
Player.rotationY =- 5;
}
function movebullet(e:Event):void{
e.target.y -= 12;
e.target.x -=96;/*When the function is called the targets Y position will be subract by 40 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.*/
if(e.target.y <= -200 && e.target.x <= -200 ){
e.target.removeEventListener(Event.ENTER_FRAME, movebullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event:Event):void {
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event:Event):void {
var bullet1:MovieClip = MovieClip(event.target);
if (bullet1.hitTestObject(mc_target)){
mc_burst.x = mc_target.y;
mc_burst.y = mc_target.x;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550){
bullet1.removeEventListener(Event.ENTER_FRAME, targeting);
}
else{
bullet1.y -= 12;
bullet1.x -= 96;}
}
// REPLACING CURSOR BY A SIGHT //
import flash.ui.Mouse;
Mouse.hide();
var myCursor:sight = new sight();
myCursor.visible = false;
function init()
{
addChild(myCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OVER, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OUT, mouseMoveHandler);
}
function mouseMoveHandler(evt:MouseEvent):void
{
myCursor.visible = true;
myCursor.x = evt.stageX + 10;
myCursor.y = evt.stageY + 10;
}
function mouseLeaveHandler(evt:Event):void
{
myCursor.visible = false;
}
init();
答案 0 :(得分:2)
假设您发布的代码是一切,问题是永远不会调用targeting
方法。
好像你想把它作为输入框架事件的处理程序添加(因为你要在方法中移除一个监听器)
例如。
bulletInstance.addEventListener(Event.ENTER_FRAME, targeting);
那就是说,查看你的代码,你会想要结合你的移动和定位功能(你不想在你的moveBullet
函数中移除子弹后继续进行碰撞检查) - 或者至少从屏幕上删除按钮时删除targeting
输入框架侦听器。
可能是这样的:
function removeBullet(b:MovieClip):void {
b.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(b));
}
function moveBullet(e:Event):void {
var bullet:MovieClip = MovieClip(event.target);
bullet.y -= 12;
bullet.x -= 96;
if(bullet.y <= -200 || bullet.x <= -200 ){
removeBullet(bullet);
}
if (bullet.hitTestObject(mc_target)){
mc_burst.x = mc_target.x;
mc_burst.y = mc_target.y;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
removeBullet(bullet);
trace("targerting");
} else if (mc_target.x > 550){
removeBullet(bullet);
}
}
如果你有很多项目符号,你可能只想拥有一个输入框处理程序,并在那里遍历每个项目符号 - 而不是为每个项目符号都有一个单独的输入框处理程序。
另外,我很惊讶你没有收到错误,因为你的命名模糊不清。您有一个名为bullet
的类,但您也可以创建名为bullet
的变量。 AS3中的标准做法是将您的班级名称作为国会大厦的第一个字母,并且您的实例命名为小写的第一个字母。我建议你这样做,以避免错误和模糊的代码。
答案 1 :(得分:-1)
我想感谢BadFeelingAboutThis在这里的快速帮助。
因此,对于想要使用我的代码的人来说,现在就开始工作
var Player:gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e:MouseEvent):void{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event:Event):void{
var bullet1:bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
bullet1.x = Player.x;
bullet1.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(bullet1);
/* addChild(explosion);*/
bullet1.addEventListener(Event.ENTER_FRAME, targeting);
}
function release_shoot(event:Event):void{
var explosion:explo1 = new explo1();
Player.rotationX =- 5;
Player.rotationY =- 5;
}
function movebullet(e:Event):void{
e.target.y -= 12;
e.target.x -=96;/*When the function is called the targets Y position will be subract by 40 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.*/
if(e.target.y <= -200 && e.target.x <= -200 ){
e.target.removeEventListener(Event.ENTER_FRAME, movebullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event:Event):void {
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event:Event):void {
var bullet1:MovieClip = MovieClip(event.target);
if (bullet1.hitTestObject(mc_target)){
mc_burst.x = mc_target.y;
mc_burst.y = mc_target.x;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550){
bullet1.removeEventListener(Event.ENTER_FRAME, targeting);
}
else{
bullet1.y -= 12;
bullet1.x -= 96;}
}
// REPLACING CURSOR BY A SIGHT //
import flash.ui.Mouse;
Mouse.hide();
var myCursor:sight = new sight();
myCursor.visible = false;
function init()
{
addChild(myCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OVER, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OUT, mouseMoveHandler);
}
function mouseMoveHandler(evt:MouseEvent):void
{
myCursor.visible = true;
myCursor.x = evt.stageX + 10;
myCursor.y = evt.stageY + 10;
}
function mouseLeaveHandler(evt:Event):void
{
myCursor.visible = false;
}
init();