I am Tookman, a high school student that doesn't have much experience coding games, but I have experience with just general coding, can someone give me the coding to a simple game so that I can study it to understand more about game coding?
Okay, i almost have the shark in there, but there is one error on the line that states public function main void (the second time), what is wrong?;
package
{ import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard public class Main extends Sprite
{ private var aDown:Boolean = false; private var dDown:Boolean = false; private var sDown:Boolean = false; private var wDown:Boolean = false; private var char : player; public function Main():void
{ char = new player(); char.x = 500; char.y = 300; addChild(char); stage.addEventListener(Event.ENTER_FRAME, keys); stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown); stage.addEventListener(KeyboardEvent.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (aDown == true)
char.x -= 10;
if (dDown == true)
char.x += 10;
if (sDown == true)
char.y += 10;
if (wDown == true)
char.y -= 10;
}
public function keysdown(e:KeyboardEvent):void
{ switch (e.keyCode) { case Keyboard.A: { aDown = true; break; } case Keyboard.D: { dDown = true; break; } case Keyboard.S: { sDown = true; break; } case Keyboard.W: { wDown = true; break; } }
}
public function keysup(e:KeyboardEvent):void
{ switch (e.keyCode) { case Keyboard.A: { aDown = false; break; } case Keyboard.D: { dDown = false; break; } case Keyboard.S: { sDown = false; break; } case Keyboard.W: { wDown = false; break; } }
} private var gDown:Boolean = false; private var jDown:Boolean = false; private var hDown:Boolean = false; private var yDown:Boolean = false; private var par : shark; public function main : void();
{ char = new player(); char.x = 300; char.y = 100; addChild(char); stage.addEventListener(Event.ENTER_FRAME, keys); stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown); stage.addEventListener(KeyboardEvent.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (gDown == true)
par.x -= 10;
if (jDown == true)
par.x += 10;
if (hDown == true)
par.y += 10;
if (yDown == true)
par.y -= 10;
}
public function keysdown(e:KeyboardEvent):void
{ switch (e.keyCode) { case Keyboard.G: { gDown = true; break; } case Keyboard.J: { jDown = true; break; } case Keyboard.H: { hDown = true; break; } case Keyboard.Y: { yDown = true; break; } }
}
public function keysup(e:KeyboardEvent):void
{ switch (e.keyCode) { case Keyboard.G: { gDown = false; break; } case Keyboard.J: { jDown = false; break; } case Keyboard.H: { hDown = false; break; } case Keyboard.Y: { yDown = false; break; } }
}
}
if you are working with an img then there are a few ways to get it to appear on the screen. You can blit, copyPixel, or a simple addChild, just to name a few. The methods vary, but the general direction is embed > declare > addChild. Once you get the img to appear then the coding is exactly the same as the A and D buttons, but it will include a bit more to animate the shark.
EDIT: oop ninja'd I did not see your 2 posts there