ForumsProgramming ForumNew Programmer

128 45463
Tookman
offline
Tookman
87 posts
Nomad

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
Tookman
offline
Tookman
87 posts
Nomad

I just need to figure out how to add in that when bobs x and y variables are close to that of the speed boost, the new variable already works just fine I just need it to change bobs speed when he almost touches it.

weirdlike
offline
weirdlike
1,299 posts
Prince

have you tried this?

if(bob.hitTestObject(speedBoost))
{
speed = 20;
speedDuration = 30
}

Tookman
offline
Tookman
87 posts
Nomad

Test object... you mean the Speed class?

Tookman
offline
Tookman
87 posts
Nomad

okay so this is what I wrote
if(char.hitTestObject(Speed))
{
speed = 20;
speedDuration = 30
}
}

it says something about speed 24 Error: Implicit coercion of a value of type Class to an unrelated type flash.displayisplayObject.

Tookman
offline
Tookman
87 posts
Nomad

if(char.hitTestObject(sar))
{
speed = 20;
speedDuration = 30
}
worked but the duration doesn't work right, actually it doesn't work at all )=

weirdlike
offline
weirdlike
1,299 posts
Prince

post your code? :/

Tookman
offline
Tookman
87 posts
Nomad

wait a minute doing something else

weirdlike
offline
weirdlike
1,299 posts
Prince

@Tookman

looking back on your posted code... isnt sar the shark?

If so, then the collision wont trigger until the character hits the shark.

Tookman
offline
Tookman
87 posts
Nomad

no sar is speed par is shark... I set them all up with similar names for some reason. I'll post my code in a minute...

Tookman
offline
Tookman
87 posts
Nomad

MAIN.AS:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
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;

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 bar : Back;

private var sar : Speed;

private var oar : Over;

private var speed:int = 10;
private var speedDuration:int = 30;

private var randomX:int = Math.floor(Math.random()*12)+1;

public function Main():void
{

bar = new Back();
bar.x = -300;
bar.y = 0;
addChild(bar);

sar = new Speed();
if(randomX == 1)
{
sar.x = -100;
sar.y = 400;
}
else if(randomX == 2)
{
sar.x = 275;
sar.y = 350;
}
else if(randomX == 3)
{
sar.x = 150;
sar.y = 200;
}
else if(randomX == 4)
{
sar.x = 700;
sar.y = 50
}
else if(randomX == 5)
{
sar.x = -50;
sar.y = 300;
}
else if(randomX == 6)
{
sar.x = 100;
sar.y = 400;
}
else if(randomX == 7)
{
sar.x = 750;
sar.y = 50;
}
else if(randomX == 8)
{
sar.x = 800;
sar.y = 400;
}
else if(randomX == 9)
{
sar.x = 350;
sar.y = 400;
}
else if(randomX == 10)
{
sar.x = 650;
sar.y = 450
}
else if(randomX == 11)
{
sar.x = 150;
sar.y = 500;
}
else if(randomX == 12)
{
sar.x = 700;
sar.y = 200;
}
addChild(sar);

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 -= speed;
if (dDown == true)
char.x += speed;
if (sDown == true)
char.y += speed;
if (wDown == true)
char.y -= speed;
if(char.hitTestObject(sar))
{
speed = 20;
speedDuration = 30
}

if (gDown == true)
par.x -= 11;
if (jDown == true)
par.x += 11;
if (hDown == true)
par.y += 11;
if (yDown == true)
par.y -= 11;
if(par.hitTestObject(sar))
{
speed = 10;
speedDuration = 30
}

if(par.hitTestObject(char))
{
oar = new Over();
oar.x = -300;
oar.y = 0;
addChild(oar);
}
}

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;
}
}
}
}
}

BACK.AS:
package
{
import flash.display.MovieClip;
public class Back extends MovieClip
{
public function Back()
{
this.graphics.beginFill(0x000080, 1);
this.graphics.drawRect(this.x, this.y, 7000, 7000);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+150, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+250, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+350, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+450, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+550, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+650, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+750, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+850, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+950, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1050, this.y+500, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y+100, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y+200, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y+300, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y+400, 40, 40);
this.graphics.endFill ();
this.graphics.beginFill(0x008000, 1);
this.graphics.drawRect(this.x+1150, this.y+500, 40, 40);
this.graphics.endFill ();

}
}
}

PLAYER.AS(BOB):

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 ();
}

}

}

SHARK.AS:

package
{
import flash.display.MovieClip;
public class shark extends MovieClip
{
public function shark()
{
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x, this.y, 10, 10);
this.graphics.endFill ();
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x+1, this.y-0.5, 8, 0.5);
this.graphics.endFill ();
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x+2, this.y-1, 6, 0.5);
this.graphics.endFill ();
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x+3, this.y-2, 4, 1);
this.graphics.endFill ();
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x + 4, this.y-3, 2, 1);
this.graphics.endFill ();
this.graphics.beginFill(0x484848, 1);
this.graphics.drawRect(this.x + 4.5, this.y-3.5, 1, 1);
this.graphics.endFill ();
this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 2, this.y+2, 2, 2);
this.graphics.endFill ();
this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x +6, this.y+2, 2, 2);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x + 3, this.y+3, 1, 1);
this.graphics.endFill ();
this.graphics.beginFill(0x000000, 1);
this.graphics.drawRect(this.x +6, this.y+2, 1, 1);
this.graphics.endFill ();
this.graphics.beginFill(0xff0000, 1);
this.graphics.drawRect(this.x +2, this.y+6, 6, 2);
this.graphics.endFill ();
}
}
}

SPEED.AS:

package
{
import flash.display.MovieClip;
public class Speed extends MovieClip
{
public function Speed()
{
this.graphics.beginFill(0xffffff, 1);
this.graphics.drawCircle(this.x, this.y, 10);
this.graphics.endFill ();

}

}

}

OVER.AS:

package
{
import flash.display.MovieClip;
public class Over extends MovieClip
{
public function Over()
{
this.graphics.beginFill(0x000000, 0.9);
this.graphics.drawRect(this.x, this.y, 2000, 2000);
this.graphics.endFill ();
///This is the background it is 90% transparent

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 350, this.y + 200, 55, 55);
this.graphics.drawRect(this.x+350, this.y+255, 15, 45);
this.graphics.drawRect(this.x+350, this.y+300, 50, 15);
this.graphics.drawRect(this.x+360, this.y+210, 35, 35);
this.graphics.endFill ();
///g 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 415, this.y + 210, 45, 45);
this.graphics.drawRect(this.x+460, this.y+185, 10, 70);
this.graphics.drawRect(this.x+420, this.y+185, 40, 7.5);
this.graphics.drawRect(this.x+427.5, this.y+217.5, 30, 30);
this.graphics.endFill ();
///a 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 490, this.y + 200, 110, 55);
this.graphics.drawRect(this.x+505, this.y+210, 35, 45);
this.graphics.drawRect(this.x+550, this.y+210, 35, 45);
this.graphics.endFill ();
///m 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 615, this.y + 195, 60, 60);
this.graphics.drawRect(this.x + 622.5, this.y + 230, 52.5, 17);
this.graphics.drawRect(this.x+622.5, this.y+202.5, 45, 20);
this.graphics.endFill ();
///e 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 662, this.y + 290, 70, 70);
this.graphics.drawRect(this.x+672, this.y+300, 50, 50);
this.graphics.endFill ();
///o 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 742, this.y + 300, 40, 60);
this.graphics.drawRect(this.x+752, this.y+300, 20, 45);
this.graphics.endFill ();
///v 100%

this.graphics.beginFill(0xffffff, 1);
this.graphics.drawRect(this.x + 793, this.y + 300, 60, 60);
this.graphics.drawRect(this.x+800.5, this.y+307.5, 45, 20);
this.graphics.drawRect(this.x+800.5, this.y+335, 52.5, 17);
this.graphics.endFill ();
///e 100%

this.graphics.beginFill(0xffffff, 1)
this.graphics.drawRect(this.x + 863, this.y + 290, 15, 70);
this.graphics.drawRect(this.x + 878, this.y + 300, 25, 10);
this.graphics.endFill ();
///r 100%
}
}
}

weirdlike
offline
weirdlike
1,299 posts
Prince

yikes that's a spaghetti code if I ever saw one. :/

what you have are 2 collisions
this

and this

I am assuming they are triggering correctly, but the first collision changes the speed to 20 then the second changes it back to 10... They are both triggering at the same time.

if(char.hitTestObject(sar))
{
	speed = 20;
	speedDuration = 30
}
Tookman
offline
Tookman
87 posts
Nomad

no they aren't it's par.hitTestObject(sar) so when the player hits the speedboost he goes faster and then when the shark hits the speedboost he goes slow again.

weirdlike
offline
weirdlike
1,299 posts
Prince

ah I see... try entering a trace statement to see if it even triggers at all

if(char.hitTestObject(sar))
{
	speed = 20;
	speedDuration = 30
	trace("char hit speed")
}
Tookman
offline
Tookman
87 posts
Nomad

I can tell it triggers the change is dramatic.

Tookman
offline
Tookman
87 posts
Nomad

what is flash's version of python's print?

Showing 106-120 of 128