The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: Clean code from the start.

1. 9
posts
Registered User

## Clean code from the start.

Hi all ... I'm a first time poster and haven't quite lurked enough - but I'll certainly go off and keep researching and post my own answer if I find it before I get a response.

I've written a bit of code:
Code:
```    if (ss._x > Stage.width) {
ss._x = Stage.width;
Xspd *= -1;
} else {
if (ss._x < 0) {
ss._x = 0;
Xspd *= -1;
}
}
if (ss._y > Stage.height) {
ss._y = Stage.height;
Yspd *= -1;
} else {
if (ss._y < 0) {
ss._y = 0;
Yspd *= -1;
}
}```
SS is a movieclip that is travelling at in direction X,Y
If it hits the wall it changes direction ... (typical pong)

What I'd like to do is this:

Code:
```MovieClip.prototype.EdgeHit = function (xs, ll, ul, v) {
// Object, Axis, Lower Limit, Upper Limit, Vector
if (xs > ul) {
xs = ul;
v *= -1;
} else {
if (xs < ll) {
xs = ll;
v *= -1;
}
}
}
ss.EdgeHit(ss._x, 0,Stage.width, Xspd);
ss.EdgeHit(ss._y, 0,Stage.height, Yspd);```
The exact same thing ... but separated out

the problem being I can't seem to pass the values by reference yet ... hrmmm ... my reading has said that values are passed by reference and it just doesn't work

If anyone knows what I'm missing here please speak up!

It's really just an aesthetic thing at the moment - but I've always found that writing code in this sort of 'modular' way makes it a lot easier to maintain in the long run.

2. 9
posts
Registered User
ahh... many more ways to skin a cat!

Perhaps I can create an X/Y test function and then I can pass each object to that function instead of looking for X then Y

So maybe the question is can I add variables to each moviclip so that each one has its own XS & YS

3. I do't know if I get the point, but if you want to add variables to a MC instance you can do that even at the time you attach it.

attachMovieClip(sourceClip, name, depth, {_x: xValue, _y: yValue, XS: xsValue});

You can add any number o variables this way. But I'm feeling like this is not what you wanted and that I completely missed the point. Anyway...

4. 9
posts
Registered User
actually, that works like a charm thanks

5. You're welcome.

6. if(condition1)
{

}
else if(condition2)
{

}
else
{

}

7. 9
posts
Registered User

## Ha ha! Classes

I've stumbled across the thing I was looking for - and it's classes ... I save a file called ship.as and then each movieclip I add to the stage should have all the requires variables and methods I need (in theory).

I'm going to keep hammering away at it (and post the answer if I find it) - but if anyone knows & can see the little bits I'm doing wrong please speak up.

The eventual goal is to make the player and all enemies work as 'ships' ... and then be able to iterate through each one (dynamically created) in an array.

If I can crack the classes then projectiles and collisions follow

EDIT - I've cleaned the code to eliminate the syntax errors though I'm not quite sure if it can be used properly yet ... not using the package statement in AS2.0 I guess

Code:
```class ship extends MovieClip {
public var Xspd = 0;
public var Yspd = 0;
public var Speed = 0;
public var MaxSpeed = 10;
public var MinSpeed = -2;
public var Rspd = 15;
public var Acceleration = 1;
public function ship() {
}
public function RLeft() {
this._rotation += Rspd;
}
public function RRight() {
this._rotation -= Rspd;
}
public function forward() {
this.Speed += this.Acceleration;
this.gotoAndPlay('JetStart');
if (this.Speed>this.MaxSpeed) {
this.Speed = this.MaxSpeed;
}
this.Xspd = Math.cos((this._rotation/180)*Math.PI)*this.Speed;
this.Yspd = Math.sin((this._rotation/180)*Math.PI)*this.Speed;
}
public function backwards() {
this.Speed = -Acceleration;
this.gotoAndPlay('JetStop');
if (this.Speed<this.MinSpeed) {
this.Speed = this.MinSpeed;
}
this.Xspd = Math.cos((this._rotation/180)*Math.PI)*this.Speed;
this.Yspd = Math.sin((this._rotation/180)*Math.PI)*this.Speed;
}
}```
@Sirisian - ^^ LOL! I didn't even see that cheers! sometimes you just get "into the zone" when writing these things and don't see the odd things you've done to make them work.
Last edited by Alex.Chesser; June 5th, 2008 at 09:02 AM. Reason: code correction

8. 9
posts
Registered User
OK ... here is the 'start of' something ... the 'bare bones' are there.

in 'next' versions
- the ability for the ship to fire lazergunz
- creating an array of enemy ships each with their own speed and direction
- collisions PEW PEW!

any tips from experienced pros would be <3 lovely
Last edited by Alex.Chesser; June 5th, 2008 at 11:02 AM.

9. rrh
207
posts
Registered User
Learning some object oriented programming through classes is a very good step towards sensible coding. One aspect of this that I use over and over in games is polymorphism through inheritance.

Quick example, say I make a Monster.as class, with methods like Monster.move() and Monster.attack().

Then I make two classes that extend Monster. FlyingEyeball and Robot. Eyeballs fly and spit acid, but Robots go along the ground and shoot lasers, so for each, I override the .move() and .attack() methods to reflect different behaviour.

Then, in my primary game engine, I can make a big list of the two types of monsters, but when looping through my array of monsters, I can just call .move() and .attack() and treat all monsters the same.

I'm also really getting into using dispatchEvent and event listeners to communicate between objects, but that's bigger in as3 than as2.

Look up:
override
polymorphism

Also, when you can, plan out the general organization of the code on paper before-hand. Plan out what classes you'll have, what each class will represent, what each class needs to be capable of, what data it will need to do that, what data the class will store itself, and which data it will access from other objects, when it will access it, etc. That will mean you can see if it's sensibly organized before you delve into the specifics of how those classes will be coded.
Last edited by rrh; June 5th, 2008 at 11:36 AM.

10. 9
posts
Registered User
Aye - i'm finding out that 'missles' are really just going to be 'ships' that don't have user control
I was just thinking that I have to add an isMissle method - but - your advice is really well timed because I'd not thought about it like that.

What I'll probably do is re-write Ship so that it's named "mover" then like you've suggested make mover.ship and mover.missle then just to understand classes maybe go with mover.asteroid

Flying eyeballs that spit acid? Robots?! I want to play that game

11. gud job
Originally Posted by rrh
Learning some object oriented programming through classes is a very good step towards sensible coding. One aspect of this that I use over and over in games is polymorphism through inheritance.

Quick example, say I make a Monster.as class, with methods like Monster.move() and Monster.attack().

Then I make two classes that extend Monster. FlyingEyeball and Robot. Eyeballs fly and spit acid, but Robots go along the ground and shoot lasers, so for each, I override the .move() and .attack() methods to reflect different behaviour.

Then, in my primary game engine, I can make a big list of the two types of monsters, but when looping through my array of monsters, I can just call .move() and .attack() and treat all monsters the same.

I'm also really getting into using dispatchEvent and event listeners to communicate between objects, but that's bigger in as3 than as2.

Look up:
override
polymorphism

Also, when you can, plan out the general organization of the code on paper before-hand. Plan out what classes you'll have, what each class will represent, what each class needs to be capable of, what data it will need to do that, what data the class will store itself, and which data it will access from other objects, when it will access it, etc. That will mean you can see if it's sensibly organized before you delve into the specifics of how those classes will be coded.