Forums → Programming Forum → New Programmer
128 | 44374 |
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
Hello, sorry I haven't been on a lot lately, I was having troubles with my adobe flash player. I got the issue fixed but i do not know why my square won't move. this is my current program.
C:\Users\Matthew\Documents\Videogame!\Main.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var charlayer = new player ();
public function Main():void
{
char.x = 500;
char.y = 300;
addChild(char);
stage.addEventListener(Event.ENTER_FRAME, keys);
stage.addEventListener(KeyboardEvent.Event.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.Event.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (aDown) = true
char.x - 5;
if (dDown) = true
char.x + 5;
}
public function keysdown(e:KeyboardEvent):void
{
if (e.keyCode == 65)
aDown = true;
if (e.keyCode == 68)
dDown = true;
}
public function keysup(e:KeyboardEvent):void
{
if (e.keyCode == 65)
aDown = false;
if (e.keyCode == 68)
dDown = false;
}
}
}
And this is my player ( the square);
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 ();
}
}
}
if (aDown)
{
char.x -= 5;
}
if (dDown)
{
char.x += 5;
}
tho i like to use switch
public function keysdown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = true;
break;
}
}
}
public function keysup(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = false;
break;
}
}
}
so, weirdlike, I edited my code to have your switch and now it is saying that it needs to have a identifier in lines 22 and 24... why does it need this? And what does it need?
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var charlayer = new player ();
public function Main():void
{
char.x = 500;
char.y = 300;
addChild(char);
stage.addEventListener(Event.ENTER_FRAME, keys);
stage.addEventListener(KeyboardEvent.Event.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.Event.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (aDown) = true
char.x - 5;
if (dDown) = true
char.x + 5;
}
public function keysdown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = true;
break;
}
}
}
public function keysup(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = false;
break;
}
}
}
}
}
when you use the
if(aDown)
{
//
}
it is exactly the same as
if(aDown == true)
{
//
}
you dont need
if (aDown) = true
{
//
}
alternatively you can check to see if it is false
if(aDown == false)
{
//
}
or
if(!aDown)
{
//
}
So I did that now, but I have all these errors about how I'm using undefined variables like char. What's wrong with my code now?
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var charlayer = new player ();
public function Main():void
{
char.x = 500;
char.y = 300;
addChild(char);
stage.addEventListener(Event.ENTER_FRAME, keys);
stage.addEventListener(KeyboardEvent.Event.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.Event.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (aDown == true)
char.x - 5;
if (dDown == true)
char.x + 5;
}
public function keysdown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = true;
break;
}
}
}
public function keysup(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = false;
break;
}
}
}
}
}
I think this is the issue here
private var charlayer = new player ();
it doesn't match up with this
if (aDown == true)
char.x - 5;
it would need to be
private var char : player;
char = new player();
char.x = 500;
char.y = 300;
addChild(char);
now there's two more property errors, keyboard and event through...., here's my code;
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var aDown:Boolean = false;
private var dDown: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.Event.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.Event.KEY_UP, keysup);
}
public function keys(e:Event):void
{
if (aDown == true)
char.x - 5;
if (dDown == true)
char.x + 5;
}
public function keysdown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = true;
break;
}
}
}
public function keysup(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = false;
break;
}
}
}
}
}
You might want to put it into your flash develop to see what that other error says
You know weirdlike, You'd probably get more views on your YouTube if you did videos of you coding, people would love that.
import keyboard
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
and change
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.KEY_UP, keysup);
look at the lines carefully you accidentally added "Event" after KeyboardEvent
You must be logged in to post a reply!