Reputation: 197
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's been hours and I still don't understand what is the problem.
Here's the problem :
[URL=http://fr.tinypic.com/r/i5ry1i/8]My Video[/URL]
Images :
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 bullet:Bullet = new Bullet(hero.mc.x, hero.mc.y, playerDirection);
addChild(bullet);
trace("addChild Bullet");
bullet.addEventListener(Event.REMOVED, bulletRemoved);
bulletList.push(bullet);
}
In my Bullet.as :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Bullet extends MovieClip {
private var speed:int = 30;
private var initialX:int;
public function Bullet(heroX:int, heroY:int, playerDirection:String) {
trace("bullet called");
trace(heroY);
trace(heroX);
// constructor code
if(playerDirection == "left") {
speed = -30; //speed is faster if player is running
x = heroX - 25;
} else if(playerDirection == "right") {
speed = 30;
x = heroX + 25
}
y = heroY;
initialX = x; //use this to remember the initial spawn point
}
Do you know what could be the problem in my code ?
Thank you very much for your help.
EDIT
Here's the change I've made :
In my Main.as
public function fireBullet():void
{
var heroP:Point = new Point(hero.mc.x, hero.mc.y);
heroP = localToLocal(hero.mc, this, heroP);
var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
addChild(bullet);
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));
}
I've made no change in my Bullet.as
2nd EDIT
I've put some "trace" in order to understand what is happening, and I must say that I don't understand at all ! The bullet is still not aligned !
In my Main.as I've put "trace(hero.mc.y)
" in the 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);
}
In my Bullet.as I've put "trace(heroPXY)".
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 ??
Upvotes: 0
Views: 310
Reputation: 6258
You may have to convert coordinate space e.g. by using following function:
/**
* <p>converts coordinates of clip "from" to coordinate space of "to" clip</p>
* @param from
* @param to
* @param origin
* @return
*/
public function localToLocal(from:DisplayObject, to:DisplayObject, origin:Point):Point
{
if (!to || !from) return new Point();
return to.globalToLocal(from.localToGlobal(origin));
}
Usage:
var heroP:Point = new Point(hero.mc.x, hero.mc.y);
heroP = localToLocal(here.mc, this, heroP);
var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
addChild(bullet);
This should convert hereo's mc coordinate space into the coordinate space of this
where you add this bullet.
Upvotes: 1