@zachwlewis @miguelmurca @Hussein_Tammam @DarkHyudrA Ok another problem after i fixed everything and made some tweaks but now all of a sudden it runs with no error but stuff that was working before now doesn’t work (the particle explosion doesn’t work, enemys don’t shoot, the points aren’t counted,hp bar doesn’t go down, the player position is reset every time it collides with an enemy) these are probably small little mistakes i made that i keep missing no matter how many times i look through so can u guys just go through it and tell me where i messed up (and how to fix if u know how)
Player Class:
package
{
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.FP;
public class Player extends Entity
{
[Embed(source = "../assets/R.png")]
private const PLAYER:Class;
public var image:Image;
private var bulletDelay:Number = 0;
public function Player()
{
super(42, 310); //initial x and y
image = new Image(PLAYER);
graphic = image;
setHitbox(20, 20, 0, 0);
type = "player";
layer = GC.LAYER_PLAYER;
}
override public function update():void
{
if (Input.check(Key.LEFT)) { x -= 5; }
if (Input.check(Key.RIGHT)) { x += 5; }
if (Input.check(Key.UP)) { y -= 5; }
if (Input.check(Key.DOWN)) { y += 5; }
if (x < 32 + FP.camera.x) x = 32 + FP.camera.x;
if (x > FP.width - 32 + FP.camera.x) x = FP.width - 32 + FP.camera.x;
if (y < 32) y = 32 ;
if (y > FP.height - 32) y = FP.height - 32;
if(image.alpha >= 1) {
if (collide("enemy", x, y)) {
x = FP.camera.x + 32;
y = FP.halfHeight;
image.alpha = 0;
}
} else {
image.alpha += FP.elapsed;
}
if(bulletDelay > 0) bulletDelay -= FP.elapsed;
else if (Input.check(Key.SPACE)) {
bulletDelay = 0.5;
var _bullet:Bullet = FP.world.create(Bullet) as Bullet;
_bullet.x = x;
_bullet.y = y;
_bullet.setMovement(400);
FP.world.add(_bullet);
}
if (collide("enemy", x, y)) {
GV.PARTICLE_EMITTER.explosion(x, y);
x = FP.camera.x + 32;
y = FP.halfHeight;
image.alpha = 0;
}
if(image.alpha >= 1) {
var enemyBullet:Ebullet = collide("bulletB", x, y) as Ebullet;
if (collide("enemy", x, y) || enemyBullet) {
GV.HP-1;
if (enemyBullet) {
enemyBullet.destroy();
}
}
} else {
image.alpha += FP.elapsed;
}
if (collide("enemy", x, y))
{
image.color = 0x000000;
}
else
{
image.color = 0xFF0000;
}
var b:Ebullet = collide("bulletB", x, y) as Ebullet;
if (b)
{
b.destroy();
}
super.update();
}
public function kill():void
{
x = FP.camera.x + 32;
y = FP.halfHeight;
image.alpha = 0;
GV.HP--;
GV.CURRENT_GUI.playerDeath();
if (GV.HP < 0) FP.world = new GameOver;
}
public function destroy():void
{
GV.PARTICLE_EMITTER.explosion(x, y);
FP.world.recycle(this);
}
}
}
World Class:
package
{
import net.flashpunk.World;
import net.flashpunk.Entity;
import flash.display.BitmapData;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.FP;
import net.flashpunk.graphics.Backdrop;
public class MyWorld extends World
{
[Embed(source = "../assets/Binary-6.gif")]
public static const LVL21_30:Class;
private var cameraSpeed:Number = 50;
private var nextSpawn:Number = 1;
private var square:Entity;
public function MyWorld()
{
GV.reset();
add(GV.CURRENT_GUI);
GV.PLAYER.x = 42;
GV.PLAYER.y = 310;
add(GV.PLAYER);
add(GV.PARTICLE_EMITTER);
var b:Backdrop = new Backdrop(LVL21_30, true, true);
b.scrollX = 0.5;
b.scrollY = 0.5;
add(new Hbar(5, 5, 100, 5, GV.PLAYER, GV.HP));
var e:Entity = new Entity;
e.graphic = b;
e.layer = GC.LAYER_BACKGROUND;
add(e);
}
override public function update():void
{
super.update();
camera.x += cameraSpeed * FP.elapsed;
GV.PLAYER.x += cameraSpeed * FP.elapsed;
nextSpawn -= FP.elapsed;
if (nextSpawn <= 0) {
nextSpawn = 1;
var _enemy:EnemyA = create(EnemyA) as EnemyA;
_enemy.x = FP.camera.x + FP.width + 100;
_enemy.y = FP.rand(FP.height);
add(_enemy);
}
}
}
}
Enemy Class:
package
{
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.FP;
public class EnemyA extends Entity
{
private var speedY:Number = 100;
private var killed:Boolean = false;
private var shootDelay:Number;
[Embed(source = "C:/Users/pico/Documents/RGB/assets/BLK.png")] public const ENEMY_A:Class;
public function EnemyA() {
graphic = new Image(ENEMY_A);
x = FP.rand(FP.screen.width);
y = FP.rand(FP.screen.height);
type = "enemy";
setHitbox(20, 20, 0, 0);
layer = GC.LAYER_ENEMY;
}
override public function update():void
{
shootDelay = 3;
if (y < 32 && speedY < 0) speedY *= -1;
if (y > FP.height - 32 && speedY > 0) speedY *= -1;
y += speedY * FP.elapsed;
if (x < FP.camera.x - 100 - width) {
destroy();
FP.world.recycle(this);
}
var _bullet:Bullet = (collide("bullet", x, y)) as Bullet;
if (_bullet) {
_bullet.destroy();
destroy();
}
if (shootDelay > 0) shootDelay -= FP.elapsed;
else {
var newBullet:Ebullet = FP.world.create(Ebullet) as Ebullet;
newBullet.x = x - halfWidth;
newBullet.y = y;
newBullet.setMovement( -100, 0);
FP.world.add(newBullet);
shootDelay = 2;
}
}
override public function added():void
{
killed = false;
}
public function destroy():void
{
if (killed) {
GV.POINTS += 100;
GV.CURRENT_GUI.updateScore();
GV.PARTICLE_EMITTER.explosion(x, y);
}
FP.world.recycle(this);
}
}
}
GUI Class:
package
{
import flash.display.BitmapData;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Spritemap;
import net.flashpunk.graphics.Text;
public class GUI extends Entity
{
private var pointsText:Text;
private var hp:Vector.<Spritemap> = new Vector.<Spritemap>;
public function GUI()
{
var glist:Graphiclist = new Graphiclist();
var guiBackground:Image = new Image(new BitmapData(560, 20, false, 0x000000));
guiBackground.scrollX = 0;
guiBackground.scrollY = 0;
guiBackground.alpha = 0.5;
glist.add(guiBackground);
pointsText = new Text("Points: 0");
pointsText.width = 200;
pointsText.x = FP.width - 200;
pointsText.y = 2;
pointsText.scrollX = 0;
pointsText.scrollY = 0;
glist.add(pointsText);
graphic = glist;
layer = GC.LAYER_GUI;
}
private var ent:Entity;
private var variable:String;
public function updateScore():void
{
pointsText.text = "Points: " + GV.POINTS;
}
public function playerDeath():void
{
if(GV.HP >= 0 && GV.HP < hp.length) hp[GV.HP].play("die");
}
}
}
HP Class:
package
{
import net.flashpunk.Entity;
import net.flashpunk.utils.*;
public class Hbar extends Entity
{
private var ent:Entity;
private var variable:int;
public function Hbar(x:Number, y:Number, w:Number, h:Number, ent:Entity, variable:int)
{
this.x = x;
this.y = y;
setHitbox(w, h);
this.ent = ent;
this.variable = variable;
layer = -10000;
}
override public function render():void
{
Draw.rect(world.camera.x+x, world.camera.y+y, width, height, 0xFF0000);
Draw.rect(world.camera.x+x, world.camera.y+y, width*ent[variable], height, 0x00FF00);
}
}
}
Particle Controller Class:
package
{
import flash.display.BitmapData;
import flash.display.ColorCorrection;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Emitter;
import net.flashpunk.utils.Ease;
import net.flashpunk.FP;
public class ParticleController extends Entity
{
private var _emitter:Emitter;
public function ParticleController()
{
_emitter = new Emitter(new BitmapData(3, 3, false, 0xFFFFFF), 3, 3);
_emitter.newType("explosion", [0]);
_emitter.setMotion("explosion", 0, 100, 2, 360, -40, 1, Ease.quadOut);
_emitter.setAlpha("explosion", 1, 0.1);
graphic = _emitter;
layer = GC.LAYER_PARTICLES;
}
public function explosion(_x:Number, _y:Number, particles:int = 20 ):void
{
for (var i:uint = 0; i < particles; i++) {
_emitter.emit("explosion", _x, _y);
}
}
}
}
Bullet Class:
package
{
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Spritemap;
public class Bullet extends Entity
{
[Embed(source = "../assets/bullet.png")]
public static const BULLET:Class;
private var sprites:Spritemap;
private var moveX:Number = 0;
private var moveY:Number = 0;
public function Bullet()
{
sprites = new Spritemap(BULLET, 10, 10);
sprites.add("idle", [0, 1, 2], 12, true);
sprites.play("idle");
graphic = sprites;
setHitbox(10, 10);
type = "bullet";
layer = GC.LAYER_BULLETS;
}
public function setMovement(_x:Number = 0, _y:Number = 0):void
{
moveX = _x;
moveY = _y;
}
override public function update():void
{
x += moveX * FP.elapsed;
y += moveY * FP.elapsed;
if (x > FP.camera.x + FP.width + 50 || x < FP.camera.x - 50 || y < - 50 || y > FP.height + 50) {
destroy();
}
}
public function destroy():void
{
FP.world.recycle(this);
}
}
}
Enemy Bullet Class:
package
{
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Spritemap;
public class Ebullet extends Entity
{[Embed(source = "../assets/Ebullet.png")] public static const EB_1:Class;
private var _sprites:Spritemap;
private var moveX:Number = 0;
private var moveY:Number = 0;
public function Ebullet()
{
_sprites = new Spritemap(EB_1, 10, 10);
_sprites.add("idle", [0, 0, 0, 0, 1, 2, 3, 2, 1], 12, true);
_sprites.play("idle");
_sprites.centerOO();
graphic = _sprites;
type = "bulletB";
setHitbox(8,8,4,4);
layer = GC.LAYER_BULLETS;
}
public function setMovement(_x:Number = 0, _y:Number = 0):void
{
moveX = _x;
moveY = _y;
}
override public function update():void
{
x += moveX * FP.elapsed;
y += moveY * FP.elapsed;
if (x > FP.camera.x + FP.width + 50 || x < FP.camera.x - 50 || y < - 50 || y > FP.height + 50) {
destroy();
}
}
public function destroy():void
{
FP.world.recycle(this);
}
}
}