@ thez3r0312
to enlighten you further, I updated my Code a little :
It can shoot right and left nowhere is again the tab for your program named Bullet:
Bullet is used as a class by an ArrayList within player
The trick is to extend the constructor with speedX and speedY in the class and in
tab player depending on
if (isFacingRight) give
speedX a value +4.2 or -4.2.
Also the barrel of the gun (where the bullet starts) has a small offset of +12 or -12.
The other tabs stay unchanged.
See bold parts / search new2 for changesin the constructor
- // Bullet
- // Simple class
- // http://www.processing.org/learning/topics/arraylistclass.html
- class Bullet {
- float x;
- float y;
- float speedX ;
- float speedY;
- //float gravity;
- float w;
- float life = 255;
- Bullet(float tempX, float tempY, // new2
- float tempSpeedX, float tempSpeedY,
- float tempW) {
- x = tempX;
- y = tempY;
- w = tempW;
- speedX = tempSpeedX; // 4.2; // new2
- speedY = tempSpeedY; // 0;
- //gravity = 0.1;
- }
- void move() {
- // Add speed to location
- x = x + speedX;
- y = y + speedY;
- // collision
- if (get(int(x), int(y)) == color(60) ) {
- println("##########################################");
- life=-1;
- }
- }
- boolean finished() {
- // fade out
- // life--;
- // bullet dead?
- if (life < 0) {
- return true;
- }
- else {
- return false;
- }
- }
- void display() {
- // Display the circle
- fill(244, 2, 2);
- noStroke();
- //stroke(0,life);
- ellipse(x, y, w, w);
- // image( ammo_fired, x, y );
- }
- }
___________________________________________________________________________________________
and the tab player :
- //
- //
- class Player {
- ArrayList<Bullet> bullets; // new1
- PVector position, velocity; //should contain x and y...
- Boolean isOnLand; // is player on the ground?
- Boolean isFacingRight; // is player facing right, used for flipping image
- Boolean shoot;
- int animationDelay;
- int animationFrame;
- int coinsCollected;
- //bullets = new ArrayList();
- static final float JUMP_POWER = 10.0; //how fast player goes up
- static final float RUN_SPEED = 5.0; //how fast player moves
- static final float AIR_SPEED = 1.5; //affects movement in air
- static final float SLOWDOWN_PREC = 0.4; //friction while on ground
- static final float AIR_SLOWDOWN_PREC = 0.7; //friction while in air, makes it so player doesn't move super fast
- static final float STAND_SPEED = 1.0; //this is the speed for when a player isn't moving, not sure if needed
- static final int RUN_ANIMATION_DELAY = 3; //frames till animation updates
- Player() {
- bullets=new ArrayList(); // new1
- shoot = true;
- isOnLand = false;
- isFacingRight = true;
- position = new PVector();
- velocity = new PVector();
- reset();
- }
- void reset() {
- coinsCollected = 0;
- animationDelay = 0;
- animationFrame = 0;
- velocity.x = 0;
- velocity.y = 0;
- }
- void inputCheck() {
- float speedsHere = (isOnLand ? RUN_SPEED : AIR_SPEED);
- float frictionsHere = (isOnLand ? SLOWDOWN_PREC : AIR_SLOWDOWN_PREC);
- if (theKeyboard.holdS && theKeyboard.holdRight) {
- velocity.x += speedsHere*2;
- }
- if (theKeyboard.holdS && theKeyboard.holdLeft) {
- velocity.x -= speedsHere*2;
- }
- if (theKeyboard.holdSpace) { // new1
- fire();
- }
- if (theKeyboard.holdLeft) {
- velocity.x -= speedsHere;
- }
- else if (theKeyboard.holdRight) {
- velocity.x += speedsHere;
- }
- velocity.x *= frictionsHere; // this should cause player to constantly be losing speed...
- if (isOnLand) { //player can only jump if on ground, change this for a double jump ability
- if (theKeyboard.holdUp) {
- velocity.y = -JUMP_POWER;
- isOnLand = false;
- }
- }
- }
- void checkForWalls() {
- int charWidth = character_stand.width; // image size
- int charHeight = character_stand.height;
- int wallDistance = int(charWidth * 0.5);
- int ceilingDistance = int(charHeight * 0.95);
- // detect wall/ceiling bumps
- PVector rightLow, rightHigh, leftLow, leftHigh, topSide;
- leftHigh = new PVector();
- rightHigh = new PVector();
- leftLow = new PVector();
- rightLow = new PVector();
- topSide = new PVector();
- //update
- leftHigh.x = leftLow.x = position.x - wallDistance; // left edge of player
- rightHigh.x = rightLow.x = position.x + wallDistance; // right edge of player
- leftLow.y = rightLow.y = position.y-0.3 * charHeight; // shin high
- leftHigh.y = rightHigh.y = position.y-0.8 * charHeight; // shoulder high
- topSide.x = position.x; // center of char
- topSide.y = position.y-ceilingDistance; // top of char
- //kill player and reset game
- if (theWorld.worldSquareAt(topSide) == World.TILE_KILLBLOCK ||
- theWorld.worldSquareAt(leftHigh) == World.TILE_KILLBLOCK ||
- theWorld.worldSquareAt(leftLow) == World.TILE_KILLBLOCK ||
- theWorld.worldSquareAt(rightHigh) == World.TILE_KILLBLOCK ||
- theWorld.worldSquareAt(rightLow) == World.TILE_KILLBLOCK ||
- theWorld.worldSquareAt(position) == World.TILE_KILLBLOCK)
- {
- gameover = true;
- return;
- }
- if (theWorld.worldSquareAt(topSide)==World.TILE_SOLID || theWorld.worldSquareAt(topSide)==World.TILE_SOLID1) {
- if (theWorld.worldSquareAt(position)==World.TILE_SOLID || theWorld.worldSquareAt(topSide)==World.TILE_SOLID1) {
- position.sub(velocity);
- velocity.x=0.0;
- velocity.y=0.0;
- }
- else {
- position.y = theWorld.bottomOfSquare(topSide)+ceilingDistance;
- if (velocity.y < 0) {
- velocity.y = 0.0;
- }
- }
- }
- if ( theWorld.worldSquareAt(leftLow)==World.TILE_SOLID || theWorld.worldSquareAt(leftLow)==World.TILE_SOLID1) {
- position.x = theWorld.rightOfSquare(leftLow)+wallDistance;
- if (velocity.x < 0) {
- velocity.x = 0.0;
- }
- }
- if ( theWorld.worldSquareAt(leftHigh)==World.TILE_SOLID || theWorld.worldSquareAt(leftHigh)==World.TILE_SOLID1) {
- position.x = theWorld.rightOfSquare(leftHigh)+wallDistance;
- if (velocity.x < 0) {
- velocity.x = 0.0;
- }
- }
- if ( theWorld.worldSquareAt(rightLow)==World.TILE_SOLID || theWorld.worldSquareAt(rightLow)==World.TILE_SOLID1) {
- position.x = theWorld.leftOfSquare(rightLow)-wallDistance;
- if (velocity.x > 0) {
- velocity.x = 0.0;
- }
- }
- if ( theWorld.worldSquareAt(rightHigh)==World.TILE_SOLID || theWorld.worldSquareAt(rightHigh)==World.TILE_SOLID1) {
- position.x = theWorld.leftOfSquare(rightHigh)-wallDistance;
- if (velocity.x > 0) {
- velocity.x = 0.0;
- }
- }
- }
- void checkForCoinCollect() {
- PVector centerOfPlayer;
- PVector topOfPlayer;
- centerOfPlayer = new PVector(position.x, position.y - character_stand.height/2);
- topOfPlayer = new PVector(position.x, position.y - character_stand.height);
- if (theWorld.worldSquareAt(centerOfPlayer) == World.TILE_COIN) {
- theWorld.setSquareAtToThis(centerOfPlayer, World.TILE_EMPTY);
- coinsCollected++;
- }
- if (theWorld.worldSquareAt(topOfPlayer) == World.TILE_COIN) {
- theWorld.setSquareAtToThis(topOfPlayer, World.TILE_EMPTY);
- coinsCollected++;
- }
- }
- void checkFalling() {
- if (theWorld.worldSquareAt(position)==World.TILE_EMPTY ||
- theWorld.worldSquareAt(position)==World.TILE_COIN) {
- isOnLand=false;
- }
- if (isOnLand == false) {
- if (theWorld.worldSquareAt(position)==World.TILE_SOLID || theWorld.worldSquareAt(position)==World.TILE_SOLID1) { // landed on solid square?
- isOnLand = true;
- position.y = theWorld.topOfSquare(position);
- velocity.y = 0.0;
- }
- else { // fall
- velocity.y += GRAVITY_STRENGTH;
- }
- }
- }
- void move() {
- position.add(velocity);
- checkForWalls();
- checkForCoinCollect();
- checkFalling();
- }
- void fire() { // new2
- println("++++++++++++++++++++++++++++++++++++++");
- if (isFacingRight)
- {
- bullets.add ( new Bullet( position.x+12, position.y-24, 4.2, 0.0, 5 ));
- }
- else
- {
- bullets.add ( new Bullet( position.x-12, position.y-24, -4.2, 0.0, 5 ));
- }
- }
- void draw() {
- int guyWidth = character_stand.width;
- int guyHeight = character_stand.height;
- if (velocity.x < -STAND_SPEED) {
- isFacingRight = false;
- }
- else if (velocity.x > STAND_SPEED) {
- isFacingRight = true;
- }
- pushMatrix();
- translate(position.x, position.y);
- if (isFacingRight == false) {
- scale(-1, 1);
- }
- translate(-guyWidth / 2, -guyHeight); // drawing images centered on character's feet
- if (isOnLand == false) {
- image(character_run1, 0, 0);
- if (theKeyboard.holdSpace) {
- image(character_jumpshoot, 0, 0);
- }
- }
- else if (abs(velocity.x) < STAND_SPEED) {
- image(character_stand, 0, 0);
- }
- else {
- if (animationDelay--<0) {
- animationDelay=RUN_ANIMATION_DELAY;
- if (animationFrame==0) {
- animationFrame=1;
- }
- else {
- animationFrame=0;
- }
- }
- if (animationFrame==0) {
- image(character_run1, 0, 0);
- }
- else {
- image(character_run2, 0, 0);
- }
- }
- if (isOnLand == true) {
- if (theKeyboard.holdSpace) {
- if (!theKeyboard.holdLeft) {
- if (!theKeyboard.holdRight) {
- image(character_shoot, 0, 0);
- //image(ammo_fired, 0, 0);
- }
- }
- }
- }
- popMatrix(); // undoes all
- for (Bullet currentBullet : bullets ) { // new1
- println ("************************");
- currentBullet.move();
- currentBullet.display();
- }
- // With an array, we say length, with an ArrayList, we say size()
- // The length of an ArrayList is dynamic
- // Notice how we are looping through the ArrayList backwards
- // This is because we are deleting elements from the list
- for (int i = bullets.size()-1; i >= 0; i--) {
- // An ArrayList
- Bullet currentBullet = bullets.get(i);
- if (currentBullet.finished()) {
- // Items can be deleted with remove()
- bullets.remove(i);
- }
- }
- }
- }
Greetings, Chrisir