Ship rotate with mouse and shootExample Shown!!
19 Replies - 8907 Views - Last Post: 11 August 2008 - 09:35 PM
#1
Ship rotate with mouse and shoot
Posted 09 August 2008 - 11:38 AM
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.
Replies To: Ship rotate with mouse and shoot
#2
Re: Ship rotate with mouse and shoot
Posted 09 August 2008 - 03:27 PM
While you posted your game it is of no use to anyone trying to help you because we can't see the source code.
#3
Re: Ship rotate with mouse and shoot
Posted 09 August 2008 - 07:22 PM
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!
#4
Re: Ship rotate with mouse and shoot
Posted 09 August 2008 - 08:56 PM
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
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.
#5
Re: Ship rotate with mouse and shoot
Posted 09 August 2008 - 09:51 PM
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
#6
Re: Ship rotate with mouse and shoot
Posted 09 August 2008 - 11:45 PM
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);
}
}
#7
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 06:22 AM
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
#8
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 07:33 AM
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
#9
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 10:17 AM
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.
#10
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 10:32 AM
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.
#11
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 10:52 AM
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.
#12
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 01:18 PM
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
#13
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 02:11 PM
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.
#14
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 02:47 PM
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..
#15
Re: Ship rotate with mouse and shoot
Posted 10 August 2008 - 05:34 PM
|
|

New Topic/Question
Reply



MultiQuote





|