Be a Supporter!

Help with code please?

  • 335 Views
  • 9 Replies
New Topic Respond to this Topic
RyanGeezy
RyanGeezy
  • Member since: Dec. 21, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Help with code please? 2012-12-21 22:03:44 Reply

Basically I have auto-turrets shooting at enemies. The turrets are in different locations, and whenever they are shot the bullets all shoot the same direction towards the enemy. They do not come to a point and cross.I created a quick line draw test toward the bottom of this code to see if the points I'm using are correct. When the bullets are shot,the lines draw exactly how they should, coming to a point where the enemy x and y is. The bullets shoot in the right direction, but not at a single point. Can anyone tell me what I am doing wrong.
Any Variables that start with '_' are arrays

for (var i:int = 0; i<Main._shipBuild.length; i++)
{
	if (Main._shipBuild[i] is AutoTurret && Main._enemy)
			{
						Main._shipBuild[i].dx = (Main._enemy[0].x + Main._enemy[0].width/2) - (player.x+Main._shipBuild[i].x);
						Main._shipBuild[i].dy = (Main._enemy[0].y + Main._enemy[0].height/2)- (player.y+Main._shipBuild[i].y);
						Main._shipBuild[i].r = Math.atan2(Main._shipBuild[i].dy,Main._shipBuild[i].dx);
						Main._shipBuild[i].rotation = (180 / Math.PI) * Main._shipBuild[i].r;
						var bullet:TBullet = new TBullet(Main._bullet);
						addChild(bullet);
						bullet.x = player.x+(Main._shipBuild[i].x);
						bullet.y = player.y+(Main._shipBuild[i].y);
						bullet.dx = (Main._enemy[0].x + Main._enemy[0].width/2) - bullet.x
						bullet.dy = (Main._enemy[0].y + Main._enemy[0].height/2)- bullet.y
						bullet.r = Math.atan2(bullet.dy,bullet.dx);
						bullet.rotation = (180 / Math.PI) * bullet.r;
						bullet.speedX = -(Math.cos(bullet.r) * bullet.speedT);
						bullet.speedY = -(Math.sin(bullet.r) * bullet.speedT);
						var drawing:Sprite =  new Sprite();
						addChild(drawing);
						drawing.graphics.lineStyle(1, 0xFA6C00);
						drawing.graphics.moveTo(bullet.x,bullet.y);
						drawing.graphics.lineTo((Main._enemy[0].x + Main._enemy[0].width/2),(Main._enemy[0].y + Main._enemy[0].height/2));
						
					}
				}

Thank you for your time!
Ryan G

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Help with code please? 2012-12-21 22:23:41 Reply

Gosh!! What we have here? Damn you should add spaces between the subject of each line, it is a pain in the ass to read your code. Also use functions, you have a giant repeat of common things like finding the angle between two points atan2 or distances dx and dy. Use functions to return values to you.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Help with code please? 2012-12-21 22:28:07 Reply

I cannot read this mess of incorrectly used underscores and pointless static references to Main. Why are you creating hundreds of sprites when you can just draw to the same one? Why are only half of your variable name meaningful.
I think you really need to learn how to use AS3 and OOP, that will solve a lot of problems for you.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
RyanGeezy
RyanGeezy
  • Member since: Dec. 21, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help with code please? 2012-12-22 02:06:18 Reply

You don't need to know what my variables are for. This is a math problem. You know what would be impossible to read? The entire code for the game. Which works 100% except for this. So I'm not the one that needs to learn as3 and oop.

RyanGeezy
RyanGeezy
  • Member since: Dec. 21, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help with code please? 2012-12-22 02:13:50 Reply

At 12/21/12 10:28 PM, MintPaw wrote: I cannot read this mess of incorrectly used underscores and pointless static references to Main. Why are you creating hundreds of sprites when you can just draw to the same one? Why are only half of your variable name meaningful.
I think you really need to learn how to use AS3 and OOP, that will solve a lot of problems for you.

Main is a class used to store variables that can be accessed across all classes. The _ is commonly used for representing a variable that is an array. Learn your naming conventions. The variables that you don't know are just the objects and arrays I'm using to store instances of the same object and to place the bullets on the screen in the correct location. I have a building system, so the game checks where the player turrets are and adds a bullet at that location with the right rotation and speed values. Also, atan2 is already a function. HENCE, it being part of Math (which is a class). So writing a function that calls a function would be stupid.
Thank You

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Help with code please? 2012-12-22 10:07:46 Reply

At 12/22/12 02:13 AM, RyanGeezy wrote: Main is a class used to store variables that can be accessed across all classes.

Which is not a good approach in terms of oop.

The _ is commonly used for representing a variable that is an array.

It's actually commonly used to denote private variables of a class.

At 12/22/12 02:06 AM, RyanGeezy wrote: You know what would be impossible to read? The entire code for the game.

Ignoring readability is fine as long as you write on your own.
Considering your post above, you seem to have an affinity for monolithic blocks of text.
Those are not easy to read, even though you think they are, but what matters here is what the people think, who try to help you.

Also, when claiming that your code in its entirety is not readable you pretty much shoot yourself in the foot.

I for one am not sure about this statement of yours:

At 12/21/12 10:03 PM, RyanGeezy wrote: The bullets shoot in the right direction, but not at a single point.

Is the direction right or not?
Your explanation made me think having the bullets fly to one point actually is the right direction.

If the movement is not happening as desired, I'd like to see the code that moves the bullets over time.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Online!
Forum Stats
Supporter
Level 05
Game Developer
Response to Help with code please? 2012-12-22 12:21:00 Reply

At 12/22/12 02:06 AM, RyanGeezy wrote: You don't need to know what my variables are for. This is a math problem. You know what would be impossible to read? The entire code for the game. Which works 100% except for this. So I'm not the one that needs to learn as3 and oop.

did you just avoid all of his questions and then stick "So i'm not the one that needs to learn as3 and oop" on the end of it?
Seriously? You couldn't think of anything better?

Tip: If you're going to insult someone, either lay the smackdown on them or make it funny. Or both.
Cheese.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
kylelyk
kylelyk
  • Member since: May. 15, 2008
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Help with code please? 2012-12-22 19:45:17 Reply

Firstly, use some temporary variables to store objects or calculations that you will use multiple times. For instance, you could store the current enemy like:

var curEnemy:YourEnemyClass =  Main._enemy[0]

and so on.

Also, I don't see anywhere in your code where you actually store a reference to the bullet you just created. DON'T USE ANY OF THE GETCHILD FUNCTIONS TO DO THIS INSTEAD OF USING AN ARRAY. Store it in an Array or Vector.

In the constructor of your bullet class, you should put the code there that calculates all its variables by passing in starting coordinates (using Geom.Point class) and maybe an angle of travel when you first create a new bullet. This is called encapsulation (not letting parent/stranger classes willy nilly change variables) and it is your friend for making a bullet class, for example, reusable for many years to come.

Another common technique for bullets is an object pool which can automatically limit the number of bullets on screen if you want and cuts down on the amount of processing of creating new instances of a class, especially if the bullet is a MovieClip and not a Sprite.

Naming and organization conventions are essential for code that can be understood by anyone that looks at it. It is good practice doing different conventions depending on the variable, function, interface, or class. The live docs are a great place to get a feel for AS3 programming style and look up classes to see what makes them tick (possibly saving you headache in the future).

Otherwise, look around for tutorials on these topics for more information.


Kylelyk: Forwards and Backwards, Over and Out.

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Help with code please? 2012-12-23 05:10:48 Reply

At 12/22/12 02:13 AM, RyanGeezy wrote: The _ is commonly used for representing a variable that is an array. Learn your naming conventions.

Maybe you should try taking your own advice.

RyanGeezy
RyanGeezy
  • Member since: Dec. 21, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help with code please? 2012-12-24 03:08:41 Reply

Thank you for all your advice. My sources must be wrong. The scale of the project has lead me away from organization and encapsulation. I should rework some things. I didnt mean to piss everyone off, but in all fairness, the first responses I got were much less than helpful.