Hello everyone, I am making a top-down shooter game. I can't figure out how to attach an actor( Gun) to my main character(Sprite) and have the gun follow the location of the main character.
import greenfoot.*;
public class Player extends Actor
{
private Actor gun;
public void act()
{
if (gun == null && isTouching(Gun.class) && Greenfoot.isKeyDown("g"))
{
gun = getOneIntersectingObject(Gun.class);
}
...
/** at end of method */
if (gun != null && gun.getWorld() != null)
{
gun.setLocation(getX(), getY());
}
}
...
}import greenfoot.*;
public class Player extends Actor
{
private Actor gun;
public void act()
{
if (gun == null && isTouching(Gun.class) && Greenfoot.isKeyDown("g"))
{
gun = getOneIntersectingObject(Gun.class);
}
...
/** at end of method */
if (gun != null && gun.getWorld() != null)
{
gun.setLocation(getX(), getY());
}
}
...
}// imports import java.util.ArrayList; // fields private ArrayList<Actor> weapons = new ArrayList<Actor>(); private Actor drawnWeapon;
// imports import java.util.ArrayList; // fields private ArrayList<Actor> weapons = new ArrayList<Actor>(); private Actor drawnWeapon;
private Actor weapon;
private void drawWeapon(int weaponNum)
{
switch (weaponNum)
{
case 1 : weapon = new Gun(); break;
case 2 : weapon = ???
...
}
getWorld().addObject(weapon, getX(), getY());
}
// in addedToWorld(World world) method (not in constructor)
drawWeapon(Greenfoot.getRandomNumber(5)+1);private Weapon weapon;
private Weapon weapon;
import lang.stride.*;
import java.util.*;
import greenfoot.*;
import java.util.ArrayList;
/**
*
*/
public class WeaponE extends Actor
{
Bullet1 bullet1;
private int timer;
int damage;
GreenfootImage weaponEquip;
GreenfootImage Pistol;
World currentWorld;
public ArrayList<Actor>weapons = new ArrayList<Actor>(); // How do I add weapon actor here? what is the syntax that i need to use?
public WeaponE(World world)
{
currentWorld = world;
damage = 1;
Pistol = new GreenfootImage("pistol.png");
}
/**
* Act - do whatever the pistol wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
CurrentWeapon();
if(mouse != null)
{
Vector2D weaponToMouse = new Vector2D(mouse.getX()-getX(), mouse.getY()-getY());
alignWithVector(weaponToMouse);
int buttonNumber = mouse.getButton();
if (timer > 0 ) timer --;
if (timer == 0 && buttonNumber == 1)
{
timer = 30;
bullet1 = new Bullet1();
getWorld().addObject(bullet1, getX(), getY());
bullet1.setRotation(getRotation());
}
}
}
public void alignWithVector(Vector2D v)
{
double adjacent = v.getX();
double opposite = v.getY();
double angleRadians = Math.atan2(opposite, adjacent);
double angleDegrees = Math.toDegrees(angleRadians);
setRotation((int) angleDegrees);
}
public void CurrentWeapon()
{
setImage(Pistol);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Shop here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Shop extends World
{
Counter counter;
MyWorld myWorld;
World previousWorld;
Back back = new Back();
Buy buy;
Buy buy1;
Buy buy2;
Buy buy3;
Buy buy4;
/**
* Constructor for objects of class Shop.
*
*/
public Shop(World pWorld)
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
addObject(back,130,550);
previousWorld = pWorld;
getBackground().drawImage(new GreenfootImage("$5000", 20, Color.BLACK, null), 680, 80);
getBackground().drawImage(new GreenfootImage("$3500", 20, Color.BLACK, null), 680, 200);
getBackground().drawImage(new GreenfootImage("$500", 20, Color.BLACK, null), 680, 320);
getBackground().drawImage(new GreenfootImage("+10hp, $500", 20, Color.BLACK, null), 660, 490);
getBackground().drawImage(new GreenfootImage("Welcome hero! \n Come and spend your cash!", 20, Color.BLUE, null), 40, 300);
prepare();
//next();
}
public void act()
{
back.nextLevel(previousWorld);
purchase();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
/*Buy*/ buy = new Buy();
addObject(buy,693,535); //Health potion $500
/*Buy*/ buy2 = new Buy();
addObject(buy2,702,377); // Ak47 $500
/*Buy*/ buy3 = new Buy();
addObject(buy3,703,249); // MiniMachinegun $3500
/*Buy*/ buy4 = new Buy();
addObject(buy4,702,127); // rpg $5000
}
public void purchase() // this is the method that buy the guns in the shop, not sure what to code here.
{
if(Greenfoot.mouseClicked(buy))
{
}
else if(Greenfoot.mouseClicked(buy2))
{
}
else if(Greenfoot.mouseClicked(buy3))
{
}
else if(Greenfoot.mouseClicked(buy4))
{
}
}
}
weapons.add(new Pistol());
weapons.add(new Pistol());
public void purchase() // this is the method that buy the guns in the shop, not sure what to code here.
{
if(Greenfoot.mouseClicked(buy))
{
WeaponE.weapons.add(new Pistol());
}
else if(Greenfoot.mouseClicked(buy2))
{
WeaponE.weapons.add(new Ak47());
}
else if(Greenfoot.mouseClicked(buy3))
{
WeaponE.weapons.add(new shotGun());
}
else if(Greenfoot.mouseClicked(buy4))
{
....
}
}
}// field
private int index = -1;
// method
public Actor getNext()
{
if (weapons.isEmpty()) return null;
index = (index+1)%weapons.size();
return weapons.get(index);
}// field
private int index = -1;
// method
public Actor getNext()
{
if (weapons.isEmpty()) return null;
index = (index+1)%weapons.size();
return weapons.get(index);
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Write a description of class Shop here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Shop extends World
{
Counter counter;
World previousWorld;
Back back = new Back();
Buy buy;
Buy buy1;
Buy buy2;
Buy buy3;
Buy buy4;
HealthBar increaseHealth;
public ArrayList<Actor>weapons = new ArrayList<Actor>();
Pistol pistolGun = new Pistol();
Ak47 ak47Gun = new Ak47();
Rpg rpgGun = new Rpg();
machineGun mGun = new machineGun();
private int index = -1;
/**
* Constructor for objects of class Shop.
*
*/
public Shop(World pWorld, HealthBar hp, Counter counter)
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
addObject(back,130,550);
previousWorld = pWorld;
this.increaseHealth = hp;
this.counter = counter;
getBackground().drawImage(new GreenfootImage("$5000", 20, Color.BLACK, null), 680, 80);
getBackground().drawImage(new GreenfootImage("$3500", 20, Color.BLACK, null), 680, 200);
getBackground().drawImage(new GreenfootImage("$500", 20, Color.BLACK, null), 680, 320);
getBackground().drawImage(new GreenfootImage("+10hp, $500", 20, Color.BLACK, null), 660, 490);
getBackground().drawImage(new GreenfootImage("Welcome hero! \n Come and spend your cash!", 20, Color.BLUE, null), 40, 300);
prepare();
//next();
}
public Actor getNext()
{
if (weapons.isEmpty()) return null;
index = (index+1)%weapons.size();
return weapons.get(index);
}
public void act()
{
back.nextLevel(previousWorld);
purchase();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
/*Buy*/ buy = new Buy();
addObject(buy,693,535); //Health potion $500
/*Buy*/ buy2 = new Buy();
addObject(buy2,702,377); // Ak47 $500
/*Buy*/ buy3 = new Buy();
addObject(buy3,703,249); // MiniMachinegun $3500
/*Buy*/ buy4 = new Buy();
addObject(buy4,702,127); // rpg $5000
}
public void purchase()
{
Message msg1 = new Message();
Message msg2 = new Message();
Message msg3 = new Message();
Message msg4 = new Message();
if(Greenfoot.mouseClicked(buy))
{
if(counter.money >= 500 && Greenfoot.mouseClicked(buy))
{
this.increaseHealth.health+=10;
counter.money-=500;
msg2.setImage(new GreenfootImage("-500 ", 25, Color.RED, null, null));
addObject(msg2, 650, 450);
Greenfoot.delay(50);
removeObject(msg2);
}
else if(counter.money < 500 && Greenfoot.mouseClicked(buy))
{
removeObject(msg1);
msg2.setImage(new GreenfootImage("Not enough Money", 25, Color.RED, null, null));
addObject(msg2, 650, 450);
Greenfoot.delay(50);
removeObject(msg2);
}
}
if(Greenfoot.mouseClicked(buy2))
{
if(counter.money >= 500)
{
weapons.add(ak47Gun);
counter.money-=500;
msg3.setImage(new GreenfootImage("-500", 25, Color.RED, null, null));
addObject(msg3, 650, 300);
Greenfoot.delay(50);
removeObject(msg3);
}
else
{
msg3.setImage(new GreenfootImage("Not enough Money", 25, Color.RED, null, null));
addObject(msg3, 650, 300);
Greenfoot.delay(50);
removeObject(msg3);
}
}
else if(Greenfoot.mouseClicked(buy3))
{
if(counter.money >= 3500)
{
weapons.add(mGun);
counter.money -= 3500;
msg4.setImage(new GreenfootImage("-3500", 25, Color.RED, null, null));
addObject(msg4, 650, 180);
Greenfoot.delay(50);
removeObject(msg3);
}
else
{
weapons.add(mGun);
msg4.setImage(new GreenfootImage("Not enough Money", 25, Color.RED, null, null));
addObject(msg4, 650, 180);
Greenfoot.delay(50);
removeObject(msg3);
}
}
else if(Greenfoot.mouseClicked(buy4))
{
if (counter.money >= 5000)
{
weapons.add(rpgGun);
counter.money -= 5000;
msg1.setImage(new GreenfootImage("-5000", 25, Color.RED, null, null));
addObject(msg1, 650, 50);
Greenfoot.delay(50);
removeObject(msg1);
}
else
{
msg1.setImage(new GreenfootImage("Not enough Money", 25, Color.RED, null, null));
addObject(msg1, 650, 50);
Greenfoot.delay(50);
removeObject(msg1);
}
}
}
}
import lang.stride.*;
import java.util.*;
import greenfoot.*;
public class Player extends Actor
{
private GreenfootImage teleport;
//private ArrayList<Actor> weapons = new ArrayList<Actor>();
//private Actor drawnWeapon;
private Actor weapon;
public int time =0;
public Player()
{
teleport = new GreenfootImage("dash.png");
}
public void act()
{
time++;
movement();
Weapon1();
hitByEnemy();
}
public void Weapon1()
{
if ( weapon == null && isTouching(Pistol.class))
{
weapon = getOneIntersectingObject(Pistol.class);
}
if (weapon !=null && weapon.getWorld() != null)
{
weapon.setLocation(getX(), getY());
}
}
public void SwitchWeapons()
{
if(Greenfoot.isKeyDown("1"))
{
}
else if(Greenfoot.isKeyDown("2"))
{
}
else if(Greenfoot.isKeyDown("3"))
{
}
else if(Greenfoot.isKeyDown("3"))
{
}
}
import lang.stride.*;
import java.util.*;
import greenfoot.*;
/**
*
*/
public class MyWorld extends World
{
int count = 0;
int spawnSpeed = 20; // lower value for faster spawnspeed
int randomSpawn = Greenfoot.getRandomNumber(8);
HealthBar healthbar = new HealthBar();
//HealthBar healthbar;
GameOver gameOver;
public Player hero = new Player();
Counter counter = new Counter();
private machineGun mGun;
Ghost ghost;
Golem golem;
Shop weaponX;
Boss boss = new Boss(hero, counter);
GreenfootSound gameplay = new GreenfootSound("GamePlay.mp3");
GreenfootSound bossBattle = new GreenfootSound("BossBattle.mp3");
/**
* Constructor for objects of class MyWorld.
*/
public MyWorld()
{
super(800, 600, 1);
addObject(hero, 400, 300);
//currentWeapon();
addObject(healthbar, hero.getX()-5, hero.getY()-50);
addObject(counter, 70, 35);
counter.score = 0;
counter.money = 0;
golem.golemKilled = 0;
ghost.ghostKilled = 0;
gameplay.play();
currentWeapon();
//machineGun mGun = new machineGun();
Pistol pistolGun = new Pistol();
addObject(pistolGun , hero.getX(), hero.getY());