PDA

View Full Version : problem with firing things different directions!


andythepandy
12-12-2007, 06:54 PM
hi there,

i need help with this very basic game i'm making.

i have code to determine which way to fire a bullet considering which side of the player the mouse is. But when i fire a bullet it goes in the correct direction but when i move the mouse over to the other side of the player, the bullet changes direction in the air!

this is my code:


level = new Array()
yspeed = 0
xspeed = 0
max_speed = 16
walk_speed = 4
gravity = 1

ball_speed = 7

can_jump = false
jumping = false
jump_power = 10

ball_released = false

_root.createEmptyMovieClip("lev",_root.getNextHighestDepth())
level[0] = new Array(1,1,1,1,1,1,1,1)
level[1] = new Array(1,0,0,0,0,0,1,1)
level[2] = new Array(1,0,1,1,1,0,0,1)
level[3] = new Array(1,0,0,0,0,0,0,1)
level[4] = new Array(1,0,0,0,0,1,0,1)
level[5] = new Array(1,0,1,1,1,0,0,1)
level[6] = new Array(1,0,0,0,0,0,1,1)
level[7] = new Array(1,1,1,1,0,0,0,1)
level[8] = new Array(1,0,0,0,0,1,1,1)
level[9] = new Array(1,0,1,0,0,0,0,1)
level[10] = new Array(1,1,1,1,0,0,0,1)
level[11] = new Array(1,0,0,0,0,0,0,1)
level[12] = new Array(1,1,1,1,1,1,1,1)
for(y = 0;y<=12;y++){
for(x = 0;x<=7;x++){
if(level[y][x] !=0){
place_brick = lev.attachMovie("block","block_"+lev.getNextHighestDepth(),lev.getNextHighestDepth (),{_x:x*20+10,_y:y*20+10})
place_brick.gotoAndStop(level[y][x])
}
}
}
_root.attachMovie("player","player",_root.getNextHighestDepth(),{_x:30,_y:40})

player.onEnterFrame = function(){
yspeed+=gravity
if(yspeed>max_speed){
yspeed = 16
}
if(Key.isDown(Key.LEFT)){
xspeed = -walk_speed
}
if(Key.isDown(Key.RIGHT)){
xspeed = walk_speed
}
if(Key.isDown(Key.UP)and can_jump){
yspeed-=jump_power
can_jump = false
}

while(_root.lev.hitTest(this._x,this._y+this._heig ht/2-0.1+yspeed,true)){
yspeed--
can_jump = true
}
while(_root.lev.hitTest(this._x-this._width/2+3+xspeed,this._y,true)){
xspeed++
}
while (_root.lev.hitTest(this._x+this._width/2+3+xspeed, this._y, true)) {
xspeed--;
}
while(_root.lev.hitTest(this._x,this._y-this._height/2+2+yspeed,true)){
yspeed++
}

this._y+=yspeed
this._x+=xspeed
xspeed = 0

}
function onMouseDown(){
xpos = player._x
ypos = player._y
cannonball_fired = attachMovie("cannonball","cannonball_"+_root.getNextHighestDepth(),_root.getNextHighestD epth(),{_x:xpos,_y:ypos})
cannonball_fired.onEnterFrame = function(){
if(_xmouse>player._x){
this._x+=ball_speed
ball_released = true
}
if(_xmouse<player._x){
this._x-=ball_speed
ball_released = true
}

if(_root.lev.hitTest(this._x,this._y,true)){
this.removeMovieClip()
ball_released = false
}

if(ball_released == true){
this._x+=10
}
}
}

i need some code to determine wether the bullet had been released and if it is true, make moving the mouse have no effect on it.

thanks for any help,
Andy

ssrun
12-13-2007, 01:49 AM
Don't perform the mouse position checks in the onEnterFrame of the cannonball clip. Perform the check once before assigning the onEnterFrame function. The onEnterFrame function should just keep the ball going in the initial direction. I'm assuming that's what you want.


function onMouseDown(){
xpos = player._x
ypos = player._y
cannonball_fired = attachMovie("cannonball","cannonball_"+_root.getNextHighestDepth(),_root.getNextHighestD epth(),{_x:xpos,_y:ypos})
if(_xmouse > player._x){
ball_released = true;
}
else if(_xmouse < player._x){
ball_speed *= -1; //assuming you want the ball to go to the left
ball_released = true;
}

cannonball_fired.onEnterFrame = function(){
if(_root.lev.hitTest(this._x,this._y,true)){
this.removeMovieClip()
ball_released = false
}

if(ball_released == true){
this._x += ball_speed;
}
}
}

andythepandy
12-14-2007, 10:55 PM
thanks for the reply,

That works for firing them in different directions but if you click on the left side again they turn around because the code said "*-1" so everytime you clicked there it times by -1. And the clicking for the right side is "disabled" because that says make the ball move at ball_speed which could still be -1.

Any suggestions would be much appreciated

Thanks,
Andy

ssrun
12-15-2007, 02:33 AM
Sorry, I forgot ball_speed is a global property. My suggestion is to make it an instance property of your cannon_ball clip. So try this code:


function onMouseDown(){
xpos = player._x
ypos = player._y
cannonball_fired = attachMovie("cannonball","cannonball_"+_root.getNextHighestDepth(),_root.getNextHighestD epth(),{_x:xpos,_y:ypos})

cannonball_fired.ball_speed = 7; //remove the global ball_speed declaration at the top of the script

if(_xmouse > player._x){
ball_released = true;
}
else if(_xmouse < player._x){
cannonball_fired.ball_speed *= -1;
ball_released = true;
}

cannonball_fired.onEnterFrame = function(){
if(_root.lev.hitTest(this._x,this._y,true)){
this.removeMovieClip()
ball_released = false
}

if(ball_released == true){
this._x += this.ball_speed;
}
}
}


Now every instance will have it's own ball_speed.

andythepandy
12-17-2007, 03:29 PM
Hi there,

thanks for the help it works fine now.

One other quick thing i may need help with is getting an enemy to move back and forwards, i don't want to animate its actions, i want it to detect if it has hit a wall an if it has chage direction.

The level is build on a movie clip called lev

Thanks for any help,
Andy

andythepandy
12-17-2007, 03:32 PM
Ok dont worry about the last thing about a moving enemy fiexed it with

if(_root.lev.hitTest(this._x,this._y,true)){
enemy_speed*=-1
}