moving image using class
in
Programming Questions
•
1 years ago
hi all i am trying to move this image but it's not working. here is my code
//define a square class for animation
class Square {
//define a square class for animation
class Square {
- /*----------properties-------*/
- float x; //x location
- float y; //y location
- float speed; // speed of movement
- int squareSize; // size of the square
- color fillColor = color (255, 128, 0); // color of square
- color strokeColor = fillColor;
- boolean xRight = true; // where true = to the right and false = to the left
- int easeFactor = 20; //amount of ease
- /*------------contructor--------------*/
- public Square(float newX, float newY, int newSize, float mySpeed) {
- x = newX;
- y = newY;
- squareSize = newSize;
- speed = mySpeed;
- }
- public void updatePosition() {
- // find the distance from the target to the square
- float distanceX = width-x;
- // ease towards the target
- float stepX = distanceX/easeFactor;
- //...if the square position + step to it won't sent it over the edge...
- if (x + squareSize + stepX < width) {
- //move it by the step
- x += stepX;
- }
- else { //othwerwise it will go over the edge, so just move to the edge
- x = width - squareSize;
- }
- }
- /*------------Methods--------------*/
- public void move (float newX, float newY) {
- newX = x;
- newY = y;
- if (xRight) { // if moving to the right
- // if the x position has gone off the screen then we reset the square to the left had side of the screen
- if (x + squareSize > width) {
- // switch directions
- xRight = false;
- }
- else {
- x+=speed; // update the x position - x = x + 1;
- }
- }
- else {
- if (x < 0) {
- // switch directions
- xRight = true;
- }
- else {
- x-=speed; // update the x position - x = x + 1;
- }
- }
- }
- public void display() {
- PImage logo;
- logo = loadImage("logo2.png");
- image(logo);
- }
- //declare a var type square to hold our square
- Square firstSquare;
- //Square secondSquare;
- //Square thirdSquare;
- //Square fourthSquare;
- //Square fifthSquare;
- int genericSize = 40;
- void setup() {
- size(400, 400);
- background(0);
- firstSquare = new Square(0, random(0, height-genericSize), genericSize, random(0, 10));
- // secondSquare = new Square(0, random(0, height-genericSize), genericSize, random(0, 10));
- // thirdSquare = new Square(0, random(0, height-genericSize), genericSize, random(0, 10));
- // fourthSquare = new Square(0, random(0, height-genericSize), genericSize, random(0, 10));
- // fifthSquare = new Square(0, random(0, height-genericSize), genericSize, random(0, 10));
- }
- void mousePressed () {
- // int easeFactor = 20;
- firstSquare.move(mouseX, mouseY);
- // easeFactor = mouseX-easeFactor;
- }
- void draw() {
- background(0);
- //draw the square
- firstSquare.display();
- //move the square
- firstSquare.move(mouseX, mouseY);
- }
1