Skip to main content
Inspiring
July 19, 2014
Question

shooting bullet in a platform game

  • July 19, 2014
  • 2 replies
  • 452 views

I'm trying to make a platform game where the player can shoot a ball.

Weirdly my code seems to shoot the bullet, but I can't figure out why it's not shooting from my player.

It seems that, when the hero is above y= 160, the bullet is well aligned with the player. when the hero y below y = 160, the bullet is not aligned.

It's been hours and I still don't understand what is the problem.

Here's the problem :

grT6D.png

vKXGP.png

Here's my code :

In my Main.as

public class Main extends MovieClip {
private var bulletList:Array = new Array();

public function keyDownFunction(event:KeyboardEvent) {
...
.
} else if (event.keyCode == 70) {
  fireBullet
();}
..
}

public function fireBullet():void
{
  
var heroP:Point = new Point(hero.mc.x, hero.mc.y);
  
var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
  heroP
= localToLocal(hero.mc, this, heroP);
  addChild
(bullet);
trace
(hero.mc.y);
  trace
("addChild Bullet");
  bullet
.addEventListener(Event.REMOVED, bulletRemoved);
  bulletList
.push(bullet);
}

public function localToLocal(from:DisplayObject, to:DisplayObject, origin:Point😞Point
{
  
if (!to || !from) return new Point();

  
return to.globalToLocal(from.localToGlobal(origin));
}


In my Bullet.as

public function Bullet(heroPX:int, heroPXY:int, playerDirection:String) {
  trace
("bullet called");
  
// constructor code
  
if(playerDirection == "left") {
  speed
= -30; //speed is faster if player is running
  x
=  heroPX;
  
} else if(playerDirection == "right") {
  speed
= 30;
  x
=  heroPX;
  
}
  y
= heroPXY;

  trace
(heroPXY);

  initialX
= x; //use this to remember the initial spawn point
  addEventListener
(Event.ENTER_FRAME, loop);
  
}


The result for trace(heroPXY) and trace(hero.mc.y) are exactly the same ! So why the bullet is not aligned ?? (especially when y>160)

(here's a video of the problem if I haven't been clear: View" rel="nofollow">http://fr.tinypic.com/r/2cx7rqx/8]View My Video )

Source File : WeTransfer

Thx for your help !


This topic has been closed for replies.

2 replies

Inspiring
July 21, 2014

1.Read this to understand how to correctly use globalToLocal/localToGlobal.

http://www.orlandmedia.com/blog/tutorials/how-to-use-localtoglobal-in-actionscript-3-0/

2..GetRid of your localToLocal method. What does it even do?

"if (!to || !from) return new Point();"


What does the negation of a DisplayObject even do?

kglad
Community Expert
Community Expert
July 19, 2014

hero's x,y will be his registration point.  if hero is nested in parent movieclips and the bullet/ball's not nested in those same movieclips, you will have additional complications.