ForumsProgramming ForumNew Programmer

128 9282
Tookman
offline
Tookman
87 posts
Peasant

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?

  • 128 Replies
weirdlike
offline
weirdlike
1,305 posts
King

there should only be 1 keyPressed function and 1 keyDepressed and 1 ENTER_FRAME, I am not sure if this is a single class or 2 classes you are working with but the function "main" should not be duplicated. Beware copy/paste. Your second main function calls for var shark but you addChild(char)

this should also be changed to make better clarity

stage.addEventListener(Event.ENTER_FRAME, keys);

addEventListener(Event.ENTER_FRAME, gameLoop);

function gameLoop(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;

if (gDown == true)
par.x -= 10;
if (jDown == true)
par.x += 10;
if (hDown == true)
par.y += 10;
if (yDown == true)
par.y -= 10;
}

Tookman
offline
Tookman
87 posts
Peasant

Sorry, that got a little confusing

Tookman
offline
Tookman
87 posts
Peasant

oh yeah I've fixed all the pars now.

Tookman
offline
Tookman
87 posts
Peasant

okay keep in mind I'm a noob, but I tried putting those new things in;
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
public class Main extends Sprite
{
private var gDown:Boolean = false;
private var jDown:Boolean = false;
private var hDown:Boolean = false;
private var yDown:Boolean = false;
private var par : shark;
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
{
par = new shark();
par.x = 280;
par.y = 300;
addChild(par);

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);
stage.addEventListener(Event.ENTER_FRAME, keys);
addEventListener(Event.ENTER_FRAME, gameLoop);
}
public function gameloop(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;
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.A:
{
aDown = true;
break;
}
case Keyboard.D:
{
dDown = true;
break;
}
case Keyboard.S:
{
sDown = true;
break;
}
case Keyboard.W:
{
wDown = true;
break;
}
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.A:
{
aDown = false;
break;
}
case Keyboard.D:
{
dDown = false;
break;
}
case Keyboard.S:
{
sDown = false;
break;
}
case Keyboard.W:
{
wDown = false;
break;
}
case Keyboard.G:
{
gDown = false;
break;
}
case Keyboard.J:
{
jDown = false;
break;
}
case Keyboard.H:
{
hDown = false;
break;
}
case Keyboard.Y:
{
yDown = false;
break;
}
}
}

}

}

THIS IS SHARK;
package
{
import flash.display.MovieClip;
public class shark extends MovieClip
{
public function shark()
{
this.graphics.beginFill(0x0c9cee, 1);
this.graphics.drawRect(this.x, this.y, 10, 10);
this.graphics.endFill ();

}

}

}

THIS IS PLAYER;
package
{
import flash.display.MovieClip;
public class player extends MovieClip
{
public function player()
{
this.graphics.beginFill(0xfff000, 1);
this.graphics.drawRect(this.x, this.y, 10, 10);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x + 3, this.y + 7, 5, 2);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x + 7, this.y + 2, 2, 2);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x + 2, this.y + 2, 2, 2);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x - 1, this.y - 1, 12, 2);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x, this.y - 4, 10, 3);
this.graphics.endFill ();
}

}

}

Tookman
offline
Tookman
87 posts
Peasant

No errors this time, but the shark doesn't even show up

Tookman
offline
Tookman
87 posts
Peasant

weirdlike?

Tookman
offline
Tookman
87 posts
Peasant

okay, now im getting errors

Tookman
offline
Tookman
87 posts
Peasant

about how par is undefined

Tookman
offline
Tookman
87 posts
Peasant

PoorBob

Tookman
offline
Tookman
87 posts
Peasant

Is the name of my game once Its made

weirdlike
offline
weirdlike
1,305 posts
King

@Tookman

representing the shark you added as the location

par = new shark();
par.x = 300;
par.y = 100;

then in the shark class you use the location of the clip

this.graphics.drawRect(this.x, this.y, 10, 10);

which makes it

graphics.drawRect(300, 100, 10, 10);

if your screen width is only 550 you would never see the rectangle as it is placed 300 pixels in on the stage and placed 300 pixels out on the sprite itself. If you wanted a 10x10 square it would be like this

graphics.drawRect(0, 0, 10, 10);

if you wanted a centered square, you could offset the size of the square to half the width.

graphics.drawRect(-5, -5, 5, 5);

this would make a 10x10 square that is centered directly where the child is added.

About your code on the previous page

stage.addEventListener(Event.ENTER_FRAME, keys);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.KEY_UP, keysup);
stage.addEventListener(Event.ENTER_FRAME, keys);
addEventListener(Event.ENTER_FRAME, gameLoop);

remove those listeners there is no function associated with them

Tookman
offline
Tookman
87 posts
Peasant

I'm sorry to say I am not a very advanced programmer and I am having trouble understanding what I need to change.

weirdlike
offline
weirdlike
1,305 posts
King

Well I see a few things, but lets start with the errors and just go from there.

x is undefined probably has to do with the structure of your game loop being public and your variables are private. You declare the character and the shark in the Main function but reference them in a separate public function.

change to

public function Main() :void
{
	addEventListener(Event.ENTER_FRAME, gameLoop);
}
public function gameLoop(e:Event):void
{
	//
}
Tookman
offline
Tookman
87 posts
Peasant

I figured it out:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
public class Main extends Sprite
{ggd
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var sDown:Boolean = false;
private var wDown:Boolean = false;
private var char : player;

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 = 500;
char.y = 300;
addChild(char);

par = new shark();
par.x = 600;
par.y = 400;
addChild(par);

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;

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.A:
{
aDown = true;
break;
}
case Keyboard.D:
{
dDown = true;
break;
}
case Keyboard.S:
{
sDown = true;
break;
}
case Keyboard.W:
{
wDown = true;
break;
}


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.A:
{
aDown = false;
break;
}
case Keyboard.D:
{
dDown = false;
break;
}
case Keyboard.S:
{
sDown = false;
break;
}
case Keyboard.W:
{
wDown = false;
break;
}


case Keyboard.G:
{
gDown = false;
break;
}
case Keyboard.J:
{
jDown = false;
break;
}

case Keyboard.H:
{
hDown = false;
break;
}
case Keyboard.Y:
{
yDown = false;
break;
}
}
}
}
}

Now how do I set up a background?

Tookman
offline
Tookman
87 posts
Peasant

Oops the bold didn't work right.

Showing 61-75 of 128