Projectile Creation without classes

Subscribe to Projectile Creation without classes 19 posts

avatar for susha susha 2 posts
Flag Post

Hi, I’m making an rpg game in as3 flash cs5 for school and I am wondering how it is possible to create projectiles firing from the hero without any external .as files, using arrays maybe?

Our teacher made us make the whole game in the main timeline with different layers for code etc which isn’t a problem until now :/.

SOMEONE PLEASE HELP!! I got the angle and direction code done, but i have no idea how to import multiple bullets in on stage and detect their collision with objects. I made a projectile movieclip as well which is sitting in the library.

 
avatar for Drakim Drakim 1183 posts
Flag Post

1. Tell your teacher he is incompetent and possibly impotent.

2. Repeat 1

“timeline” coding is not a legitimate way to structure code. Seriously, that’s horrible. Maybe next he will make you write an essay on an A3 sheet of paper? There might be some people on this forum that knows the odd ins and outs, but unfortunately I don’t.

 
avatar for DrYoshiyahu DrYoshiyahu 678 posts
Flag Post

An array is a good way to do projectiles like that, but classes are the best way to do anything. I can’t recommend strongly enough how important classes are, and you could still use arrays whilst using a class.

Here’s how the array would work:

var bullet:Bullet;
var bulletArray:Array = [];

function createBullet() {
    bullet = new Bullet();
    bulletArray.push(bullet)
}

function everyFrame() {
    for (var i in bulletArray) {
        bulletArray[i].move();
        if (bulletArray[i].hitTest(enemy)) {
            bulletArray[i].destroy();
        }
    }
}

To pick out an item in an array you use arrayName[item].
So one by one it goes through every single item, calls it “i”, and then moves it and looks for a collision.

I need to warn you though, once you have those basics down, make sure you know how to remove items from arrays, otherwise your game will lag, badly.

 
avatar for qwerber qwerber 4771 posts
Flag Post

use dynamic property giving, or whatever you want to call it.

var s:Object;
s = new Object;
s.x = 0
s.y = 0
s.velX = 0
s.velY = 0

I think you can do that directly with MovieClip.

 
avatar for GameBuilder15 GameBuilder15 8887 posts
Flag Post

What a terrible teacher.

 
avatar for BobJanova BobJanova 886 posts
Flag Post

Use classes for the love of all that is holy. Please point your teacher at this board so we can tell him exactly why using the timeline for everything is idiocy.

If it’s really absolutely a requirement then you can fake classes with the good old Javascript-style

function f(){}
f.prototype.doSomething = function(){ ... }

var myF = new f();

… and that’s probably still the best approach.

 
avatar for qwerber qwerber 4771 posts
Flag Post
Originally posted by GameBuilder15:

What a terrible teacher.

indeed D:

 
avatar for BigJM BigJM 468 posts
Flag Post
Originally posted by BobJanova:

Use classes for the love of all that is holy. Please point your teacher at this board so we can tell him exactly why using the timeline for everything is idiocy.

If it’s really absolutely a requirement then you can fake classes with the good old Javascript-style

function f(){}
f.prototype.doSomething = function(){ ... }

var myF = new f();

… and that’s probably still the best approach.

That’s still a class in the OOP sense of the term. It’s just a different way of defining one in AS3. Also, you can’t use bound functions as constructors.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3771 posts
Flag Post

Yeah, since MovieClips are dynamic, you can do something like what qwerber said:

var bullet:MovieClip = new MovieClip();
// Draw bullet
bullet.graphics.beginFill(0);
bullet.graphics.drawCircle(0,0,5);
bullet.graphics.endFill();
//Now what qwerber said
bullet.velY = 10;
bullet.velX = 20;
// Even stuff like this:
bullet.move = function(grav:Nuber):void{bullet.velY += bullet.grav; bullet.x += bullet.velX; bullet.y += bullet.velY} 
//Add to an array
bullets.push(bullet);

Then, you can do something like DrYoshiyahu said

 
avatar for Draco18s Draco18s 6879 posts
Flag Post
Originally posted by BobTheCoolGuy:

Yeah, since MovieClips are dynamic, you can do something like what qwerber said:

Then, you can do something like DrYoshiyahu said

You still, technically, end up with a class, or at least class-like, object.

 
avatar for BobJanova BobJanova 886 posts
Flag Post
That’s still a class in the OOP sense of the term. It’s just a different way of defining one in AS3.

Yeah but I think the requirement is actually ‘no external .as files’ not ‘no classes’.

Also, you can’t use bound functions as constructors.

I’ve never actually used the prototype stuff in AS, but you can do something similar to what I posted, right?

 
avatar for BigJM BigJM 468 posts
Flag Post
Originally posted by BobJanova:

Yeah but I think the requirement is actually ‘no external .as files’ not ‘no classes’.

I think it was too, but I disagreed with your term ‘fake class’ :P

Originally posted by BobJanova:

I’ve never actually used the prototype stuff in AS, but you can do something similar to what I posted, right?

Yes, with unbound functions:

var f = function(){}
f.prototype.doSomething = function(){ ... }

var myF = new f();

Most top-level classes define methods in both the prototype and the traits object to keep compliant with ES3. Also, if you’ve ever written a class in AS2, you’ve used prototype inheritance.

 
avatar for Aesica Aesica 972 posts
Flag Post

Yeah, your teacher is terrible and he/she/it is teaching you how to be a terrible flash programmer. If you decide to continue with flash after that class, I highly advise you get ready to disregard much of what he’s teaching you, then see how other people code flash applications.

Originally posted by Draco18s:
Originally posted by BobTheCoolGuy:

Yeah, since MovieClips are dynamic, you can do something like what qwerber said:


Then, you can do something like DrYoshiyahu said


You still, technically, end up with a class, or at least class-like, object.


Yeah, although something tells me that his teacher wants them to do it that way instead of the optimal way. Gross.

Edit: In addition to what DrYoshi said, don’t forget to destroy the bullets when they go off the stage (> 0, < stageWidth, < stageHeight) and, after calling destroy(), splice them out of the array.

 
avatar for Aaants Aaants 161 posts
Flag Post

Very judgmental bunch aren’t we?

It doesn’t read like he was absolutely forced to use the timeline for everything, more that the teacher started the lessons that way. If his teacher is flat out saying “never use .as files” then, ok, that’s a bit off. Saying he’s terrible (and impotent, obviously) for using the timeline is just plain wrong. If the course is anything like GCSE ICT here in England and the teacher has taken it on himself to make it interesting by introducing games, in any guise, he should be applauded.

The timeline has its place. Sure, if you want to be a programmer you should use external files but not everyone who uses Flash is/wants to be/has the capacity to be a programmer. Having taught around 100 people to use Flash, I can say that maybe 10-20% of them understood the concept of a class, let alone the implementation. After 6 weeks of learning, there are still people who simply can’t comprehend the words var and function – let alone much else. Lessons have to include a massive range of ability levels, you start at the bottom and let those with the ability progress up. Walk before you can run and all that noise.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3771 posts
Flag Post
Originally posted by Aaants:

Very judgmental bunch aren’t we?

It doesn’t read like he was absolutely forced to use the timeline for everything, more that the teacher started the lessons that way. If his teacher is flat out saying “never use .as files” then, ok, that’s a bit off. Saying he’s terrible (and impotent, obviously) for using the timeline is just plain wrong. If the course is anything like GCSE ICT here in England and the teacher has taken it on himself to make it interesting by introducing games, in any guise, he should be applauded.

The timeline has its place. Sure, if you want to be a programmer you should use external files but not everyone who uses Flash is/wants to be/has the capacity to be a programmer. Having taught around 100 people to use Flash, I can say that maybe 10-20% of them understood the concept of a class, let alone the implementation. After 6 weeks of learning, there are still people who simply can’t comprehend the words var and function – let alone much else. Lessons have to include a massive range of ability levels, you start at the bottom and let those with the ability progress up. Walk before you can run and all that noise.

I agree – for someone who has never programmed before, the timeline is a much more natural concept – when the program gets to this point, do something. And that’s all there is to it. I’ve also seen functions, a seemingly simple concept, stump people. It does sound like the teacher isn’t letting them use any external files, which is too bad.

 
avatar for BobJanova BobJanova 886 posts
Flag Post
Originally posted by Aaants:

Very judgmental bunch aren’t we?

Welcome to the Internet, you must be new here.

And while you have some good points, teaching people to do something the wrong way is a lot different from simplifying the problem.

 
avatar for Aesica Aesica 972 posts
Flag Post
Originally posted by Aaants:

The timeline has its place.

If the only things you’re creating are animations, slide shows, or button games, then absolutely. Anything beyond that is just asking for trouble. I wonder if the teacher is teaching the way he is because his experience with flash is pretty much just those 3 things…

 
avatar for GameBuilder15 GameBuilder15 8887 posts
Flag Post

But the guy’s making an RPG for the class. It doesn’t sound like an animation class. :P

 
avatar for Aesica Aesica 972 posts
Flag Post
Originally posted by GameBuilder15:

But the guy’s making an RPG for the class. It doesn’t sound like an animation class. :P

Which is exactly the point of my post. :)