Ship rotate with mouse and shoot

Example Shown!!

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 9991 Views - Last Post: 11 August 2008 - 09:35 PM

#1 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Ship rotate with mouse and shoot

Posted 09 August 2008 - 11:38 AM

Hey guys. Here is my program so far:

http://www.spotlight...st.com/Game.swf

Basically, there is a player. The cursor is supposed to rotate the player, and mouse click is supposed to shoot the bullet. WASD MOVEMENT.


My symbols:

- bullet named "basicbullet". Instance name is "bullet".
- ship named "player". Instance is "player".


How do I code these 2 things: rotation of player by moving mouse cursor, and also shooting on mouseclick.

I saw TONS of tutorials on shooting a bullet to the right or above the player, but nothing about rotational movement.
You know, basic stuff. +y or +x. I want the bullet to fire in the direction of the ship.

BASICALLY this means having the bullet always shoot out of the "cannon" you can see. Just need to be able to rotate the ship with the mouse.


Anyone able to help me with this? I gave you all the symbol names. Thanks so much.

Is This A Good Question/Topic? 0
  • +

Replies To: Ship rotate with mouse and shoot

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 09 August 2008 - 03:27 PM

You would use sine or cosine and the mouse and mc's x and y coordinates to get theangle between the mouse and movieclip and then set that as the movieclip's rotation.

While you posted your game it is of no use to anyone trying to help you because we can't see the source code.
Was This Post Helpful? 0
  • +
  • -

#3 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 09 August 2008 - 07:22 PM

Thanks betawar, Ill get to the rotating when I need to.


For now I cant get the bullet functions working.


I have a bullet1 symbol.

It has a motion tween, but frame #1 is set to stop.

On my player keypresses - with WASD movement, I have spacebar set to shoot.

The spacebar code I have is:


if (Key.isDown(Key.SPACE)) {
bullet1.gotoAndPlay(2);
}


Why is it not making my bullet1 movie play?

Frame 1 is on alpha 0%, frame2 starts the tween and is on alpha 100%.

It SHOULD moving ( the bullet ) but it isnt.

HELP!
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 09 August 2008 - 08:56 PM

First off, do you have an instance of the bullet1 on the stage with the instance name of bullet1?

If you do then it will probably work. The next thing that you may want to try is having your if statement trace out something like "Space has been Pressed!" trace("Space has been Pressed!"); to make sure that the statement is actually returning true.

Additionall make sure that your if statement is within an onEnterFrame event function, if it isn't i twill be checked once and then nothing else will be done with it.

I noticed that you say it should be moving on the second to last line

Quote

It SHOULD moving ( the bullet ) but it isnt.
but I don't see why it should be moving. From what I have read you just have a single if statement that tells the movieclip to play its tween and nothing else. So from what you have provided the movieclip shouldn't be moving, but staying in a single position. Granted it should probably be playing but there are possible reasons that it isn't doing that either.

If you try this code instead of what you currently have does it popup any messages?:
if(Key.isDown(Key.SPACE)){
  trace("Space key pressed");
  bullet1.play();
}


NOTE - You can just say bullet1.play() because it will go through and play the movieclip until it gets to another frame in which it is told to stop (which should be the first and only the first frame of the animation so it will stop when it is about to start repeating the animation).

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#5 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 09 August 2008 - 09:51 PM

Edit. Got it working.

Heres my code for player

onClipEvent (load) {
	movement = 5;
}
onClipEvent (enterFrame) 
{
//A - LEFT
if (Key.isDown( 65 )) {
	this._x = this._x - movement;
}
//D - RIGHT
if (Key.isDown( 68 )) {
	this._x = this._x + movement;
}
//W - UP
if (Key.isDown( 87 )) {
	this._y = this._y - movement;
}
//S - DOWN
if (Key.isDown( 83 )) {
	this._y = this._y + movement;

}
//SPACE BAR - SHOOT
	onmousedown = function() {
	_parent.bullet1._x = _parent.player._x + 5;
	_parent.bullet1._y = _parent.player._y;
	_parent.bullet1.gotoAndPlay(2);
}
}




Question for all you coding Gods now. How do I make multiple instances of shoot? As of now, when I move around and shoot, if I press left mouse 2 quickly, it will reset my bullet tween. I want to be able to keep moving and keep shooting.



EDIT *** heres what I got so far

onmousedown = function() {
	var newDepth:Number = _parent.getNextHighestDepth();
	var newBullet:MovieClip = _parent.attachMovie("bullet1", "bullet" + newDepth, newDepth);
	trace(newBullet);
	_parent.bullet1._x = _parent.player._x + 3;
	_parent.bullet1._y = _parent.player._y - 1;
	_parent.bullet1.gotoAndPlay(2);
}


The tracing is working correctly. bullet1, bullet2, bullet3... but it isnt actually creating new bullets.

Still just reseting the last bullet. =/ Help.

This post has been edited by Sonastylol: 09 August 2008 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 09 August 2008 - 11:45 PM

Woohoo! Bullets fire at the correct angle of the barrel now.

Now I need to figure out how to fire multiple bullets. Tracing is working correctly, but its still firing the same instance of bullet1 over and over. Any help anyone?


player code
onClipEvent (load) {
	movement = 5;
}
onClipEvent (enterFrame) 
{
//A - LEFT
if (Key.isDown( 65 )) {
	this._x = this._x - movement;
}
//D - RIGHT
if (Key.isDown( 68 )) {
	this._x = this._x + movement;
}
//W - UP
if (Key.isDown( 87 )) {
	this._y = this._y - movement;
}
//S - DOWN
if (Key.isDown( 83 )) {
	this._y = this._y + movement;

}
//(AIM) Rotating player by mouse angle
angle_in_radians = Math.atan2(_root._ymouse - this._y , _root._xmouse - this._x);
angle_in_degrees = Math.round((angle_in_radians * 180/ Math.PI));
this._rotation = angle_in_degrees;

//Left Mouse Click - Shooting
	onmousedown = function() {
	var newDepth:Number = _parent.getNextHighestDepth();
	var newBullet:MovieClip = _parent.attachMovie("bullet1", "bullet" + newDepth, newDepth);
	trace(newBullet);
	//rotates bullet to match barrel angle
	_parent.bullet1._rotation = angle_in_degrees;
	_parent.bullet1._x = _parent.player._x + 3;
	_parent.bullet1._y = _parent.player._y - 1;
	_parent.bullet1.gotoAndPlay(2);
}



}


Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 06:22 AM

The code looks correct, but you need to make sure that you have exported the movieclip from the library to work with AS in the First Frame.

Right Click on the instance of bullet1 in the Objects library and then select Linkage
Select "export for Actionscript"
then make sure that you also have "export in the first frame" checked and that the MC has the identifier "bullet1".

Should work after that.

HTH
Was This Post Helpful? 0
  • +
  • -

#8 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 07:33 AM

well it didnt work.

I realized what happened tho --> even tho I was creating new instances of bullet: bullet1, bullet2, bullet3, etc.. I was still only doing the code for bullet1.

I changed it now to

newBullet._rotation = angle_in_degrees;
newBullet._x = _parent.player._x + 3;
newBullet._y = _parent.player._y - 1;
newBullet.gotoAndPlay(2);



and this is what I get. Its creating multiple bullets, but not actually running the bullet animation that worked earlier, or killing the bullet at frame 30 when the animation tween ends. to give you more details, the motion tween is alpha 100% for frames 1-25, then frames 26-30 the bullet goes to alpha 0%


http://infinitynexus.com/Game.swf
http://infinitynexus.com/screen.bmp
Was This Post Helpful? 0
  • +
  • -

#9 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 10:17 AM

Okay, on Frame 30 (which should be a keyframe to llow this to work) add this code:

removeMovieClip(this);


That will tell it to delete itself.

I am assuming that you haven't changed anything with the bullets movieclip (If you added extra frames or something it may be stopping again because of code copies). So I am not quite sure why it is not working (doing what it did before), but you may want to look over your code and make sur ehtat you didn't change anything that you though you hadn't. That is normally the cause for these types of issues when I have them. Also make sure that you code doesn't have any errors in it, otherwise it may work to a certain point and quit.
Was This Post Helpful? 0
  • +
  • -

#10 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 10:32 AM

Betawar, thanks for the help but you missed something important.

Its not removing itself at frame 30 because it isnt getting passed frame 2.


If you run my code, you'll see that its creating the bullets but stopping them at frame 2. Teere is NO stop action on frame 2. For some reason it is failing to play.

Please help me address this. I dont understand why my actionscript isnt properly playing the bullet movie.
Was This Post Helpful? 0
  • +
  • -

#11 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 10:52 AM

Hm, I see what you are talking about. I have tried the same thing adn it just won't play. I did find a way to get it to wor though. If you get rid of the gotoAndPlay() line and the stop() line it will work fine. Seems that Flash has some bugs in their AS 2.0...

Here is what my code looks like:

onClipEvent (load) {
	movement = 5;
}
onClipEvent(enterFrame){
	//A - LEFT
	if (Key.isDown( 65 )) {
		this._x = this._x - movement;
	}
	//D - RIGHT
	if (Key.isDown( 68 )) {
		this._x = this._x + movement;
	}
	//W - UP
	if (Key.isDown( 87 )) {
		this._y = this._y - movement;
	}
	//S - DOWN
	if (Key.isDown( 83 )) {
		this._y = this._y + movement;
	}
	//(AIM) Rotating player by mouse angle
	angle_in_radians = Math.atan2(_root._ymouse - this._y , _root._xmouse - this._x);
	angle_in_degrees = Math.round((angle_in_radians * 180/ Math.PI));
	this._rotation = angle_in_degrees;

	//Left Mouse Click - Shooting
	onmousedown = function() {
		var newDepth:Number = _parent.getNextHighestDepth();
		var newBullet:MovieClip = _parent.attachMovie("bullet1", "bullet" + newDepth, newDepth);
		trace(newBullet);
		//rotates bullet to match barrel angle
		newBullet._rotation = angle_in_degrees;
		newBullet._x = _parent.player._x + 3;
		newBullet._y = _parent.player._y - 1;
	}
}


Then in the bullet movieclip I have this code on frame 30:
removeMovieClip(this);



Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#12 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 01:18 PM

Hey - it works great! Thanks so much.

I made another topic but no one was able to answer me.

I'd like to take this one .swf. Open it, have a friend open it. And have us see each other.

No bounds. No health. No collision. Just movement for now.

How can I learn about incorporating networking or winsock or WHATEVER flash uses to communicate to mysql/php.

I want this "game" as it is right now to be a bare-bones basic framework for what the game will become.


If you have any books that teach this, or online tutorials, or even keywords I can search on google, it would help a ton!

Thanks for your help thus far.

This post has been edited by Sonastylol: 10 August 2008 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 02:11 PM

Okay, I am not sure what Flash does for php communications (on the terms of sockets or what) but I know that you could use loadVariables to send and recieve things from a php/mysql server.

Here is what that looks like:

OBJECT.loadVariables("EXAMPLE.php", "GET");


Then to get the returned stuff:

OBJECT.onData = function(){
  if(this.success == true){
	//Update the screen for the other player
  }
  else{
	//There was an error and the scrip tdidn't run successfully.
  }
}


The php for this type of thing could look like so:

<?php
var $x = $_GET['tx'];
var $y = $_GET['ty'];
//PLACE TX AND TY INTO THE DATABASE

//SELECT THE OTHE RPLAYER's X AND Y FROM THE DATABASE or die("success=false\n");

//RETURN THE VALUE
echo "success=true\n
playerX=".$_RETURNED_VALUE['x']."\n
playerY=".$_RETURNED_VALUE['y'];
?>



This will probably all need to be changed up to work as you need it to but I think you will get the general idea from it. If you need more information about the loadVariables function I suggest doing a search on adobe.com.

You could do direct socketcommunications (I believe) if you were willing to buy a Flash Communications Server, but that tends to cost a couple hundred $s so it is easier to just jump through a couple of hoops.

Hope tha thelps.
Was This Post Helpful? 0
  • +
  • -

#14 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 02:47 PM

good info but out of my league.

I can't seem to dig up any tutorials on this either, sadly.

I can't believe out of so many resources and so many flash programmers that there arent any tutorials on this type of thing...

Something basic like 2 circles on a screen. One for me , one for you..
Was This Post Helpful? 1

#15 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Ship rotate with mouse and shoot

Posted 10 August 2008 - 05:34 PM

Ya, that may be my next tutorail, but I will have to play with it a while first. I am also amazed that there isn't something out there with this information I would think there would be, but I guess not...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2