PDA

View Full Version : Part 2 of Game Engine programming using classes is up



Charleh
November 30th, 2007, 10:49 AM
I posted it onto the end of the previous tutorial - there is a third part to come!

You can view the two tutorials at:

http://www.kirupa.com/forum/showthread.php?t=281507

ArmoredSandwich
November 30th, 2007, 10:53 AM
man that stuff is good xD

fw2803
November 30th, 2007, 11:56 PM
How do I apply one class on multiple objects? I combined something in your tutorial into something from some other tutorials and created this. I have a movieclip on the stage called "circle1". I exported it at linkage and I have the following codes in a .as file called "Circle.as".


class Circle extends MovieClip{
//public var xspeed
//public var yspeed
public var xspeed:Number = 0
public var yspeed:Number = 0
public var accel:Number = 2
public var maxspeed:Number = 10
public var minspeed:Number = -10
public var friction:Number = 0.9
public var circle_mc:MovieClip
public function Circle(_mc:MovieClip) {
circle_mc = _mc
}
public function moveUp() {
yspeed += accel
}
public function moveDown() {
yspeed -= accel
}
public function moveRight() {
xspeed += accel
}
public function moveLeft() {
xspeed -= accel
}
public function startMoving() {
circle_mc._x += xspeed
circle_mc._y -= yspeed
}
public function stopMoving() {
xspeed *= friction
yspeed *= friction
}
public function MaxMin() {
if (xspeed >= maxspeed) {
xspeed = maxspeed
}
if (xspeed <= minspeed) {
xspeed = minspeed
}
if (yspeed >= maxspeed) {
yspeed = maxspeed
}
if (yspeed <= minspeed) {
yspeed = minspeed
}
}
}


I have the following on the first frame of a .fla document called "class.fla" (<< but this name doesn't matter anyway, right?)


import Circle
var circle:Circle = new Circle(circle1)
onEnterFrame = function() {
if (Key.isDown (Key.UP)) {
circle.moveUp()
}
if (Key.isDown (Key.DOWN)) {
circle.moveDown()
}
if (Key.isDown (Key.RIGHT)) {
circle.moveRight()
}
if (Key.isDown (Key.LEFT)) {
circle.moveLeft()
}
circle.startMoving()
circle.MaxMin()
if ((!Key.isDown (Key.UP)) and (!Key.isDown (Key.UP)) and (!Key.isDown (Key.UP)) and (!Key.isDown (Key.UP))) {
circle.stopMoving()
}
}


//OFF TOPIC: One more thing... What is the system requirement of your game HD2, Charleh?

Charleh
December 1st, 2007, 06:47 PM
You don't apply a class to an object you create an instance of a class - I don't really know what you are trying to do - can you explain further?

My game should run quite well on any mid range PC - it probably won't run that great on older hardware - I've got an athlon XP 2500 and it runs fine on mine so anything around 4/5 years old or better should cope with it ok!

In fact my wifes celeron D laptop at 1.8 gig runs it better than this athlon!

fw2803
December 1st, 2007, 09:05 PM
Actually, I'm just trying to move a circle around the stage by using classes instead of conventional AS programming in the AS window...

And omg! My computer is nearly 10 years old... But one good thing is that if a game can ever run on my computer smoothly, everyone will be able to play it :D

hatu
December 2nd, 2007, 07:53 AM
Good stuff, keep them coming. I like reading about the same thing starting from basics by different authors, you'll usually pick some new ideas up and there's never too much info about game engines in Flash.

Charleh
December 2nd, 2007, 06:27 PM
fw2803 you can pretty much follow the first couple of sections in the tutorial and it should give you moving objects to play with!