How to make a moving picture change into another when user selects it?

edited December 2014 in How To...

Hello there! So I'm doing a game here where you get to kill ants. I've done a simple interface for the game but now I need to do is the user should "kick" the ants and the kill counter will increase and the kicked ant will change picture to something like a dead ant. I know how to do it if the ants' pictures are not moving but since they are I have no idea how to do it and therefore stuck.

simple game interface

Can you please help me? Please? :< Thanks!

 import SimpleOpenNI.*;

    SimpleOpenNI context;

    int mode = 0;
    int imNum = 3;
    int deadcount = 0;

    ArrayList <antsWalking> army = new ArrayList();
    Timer timer ;

    PImage bgi;
    PImage userImg;
    PImage homebutton;
    PImage[] ants = new PImage[imNum];
    PImage[] deadants = new PImage[imNum];

    PVector headPos = new PVector();
    PVector leftHandPos = new PVector();
    PVector rightHandPos = new PVector();
    PVector leftElbowPos = new PVector();
    PVector rightElbowPos = new PVector();
    PVector leftShoulderPos = new PVector();
    PVector rightShoulderPos = new PVector();


    PVector rightFootPos = new PVector();
    PVector leftFootPos = new PVector();
    PVector rightKneePos = new PVector();
    PVector leftKneePos = new PVector();
    PVector rightHipPos = new PVector();
    PVector leftHipPos = new PVector();


    boolean isTime = true;
    boolean countmode = false;
    int startTime = 0, endTime = 0;
    int savedTime;
    int totalTime = 2000;
    int counter;

    void setup()
    {
      context = new SimpleOpenNI(this);

      context.setMirror(true);
      context.enableDepth();
      context.enableRGB();
      context.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);

      size(640, 480);
      timer = new Timer(400, 25) ;
      timer.start() ;

      counter = millis();
      bgi = loadImage("lighthouse.jpg");
      userImg = createImage(640, 480, RGB);
      homebutton = loadImage("home.jpg");
      for (int i = 0; i < ants.length; i ++ ) 
        ants[i] = loadImage( "ant" + i + ".png" ); 
      for (int i = 0; i < ants.length; i ++ ) 
        deadants[i] = loadImage( "deadant" + i + ".png" ); 
    }


    void draw()
    {
      context.update();
      image(context.rgbImage(), 0, 0);
      int[] userList = context.getUsers();
      int passedTime = millis() - savedTime;

      for (antsWalking currItem : army) {
        currItem.display();
        currItem.move();
      }

      for (int i = army.size(); i>=0; i--) { 
        if (i<army.size()) {
          antsWalking currItem = army.get(i);
          if (!currItem.alive) { 
            army.remove(i);
          }
        }
      }

      if (passedTime > totalTime) {  
        antsWalking newAnt = new antsWalking ( int(random(0, 600)), 400, (int)random(0, 2)  ); 
        army.add( newAnt ); 
        savedTime = millis();
      }


      if (mode == 0) {
        int[] userMap = null;
        if (userList.length>0) {
          userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
          noFill();
          userImg.loadPixels();
          for (int i=0;i<userImg.pixels.length;i++)
          {
            if (userMap[i]!=0)
              userImg.pixels[i] = context.rgbImage().pixels[i];
            else
              userImg.pixels[i] = bgi.pixels[i];
          }
          userImg.updatePixels();
        }

        fill(0);
        textSize(20);
        text("Ants killed: " + deadcount, 25, 25);      
        text("Timer: ", 525, 25);
        timer.DisplayTime();


        for (int i=0;i<userList.length;i++)
        {
          if (context.isTrackingSkeleton(userList[i]))
            drawSkeleton(userList[i]);
        }
      }
    }

    void drawSkeleton(int userId)
    {
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD, headPos); //HEAD의 위치 얻어오기
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHandPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHandPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, leftElbowPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, rightElbowPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, leftShoulderPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, rightShoulderPos);

      // lower torso
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_KNEE, leftKneePos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, rightKneePos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_FOOT, leftFootPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_FOOT, rightFootPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HIP, leftHipPos);
      context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HIP, rightHipPos);

      context.convertRealWorldToProjective(headPos, headPos); //실세계 좌표에서 투영
      context.convertRealWorldToProjective(leftHandPos, leftHandPos);
      context.convertRealWorldToProjective(rightHandPos, rightHandPos);
      context.convertRealWorldToProjective(leftElbowPos, leftElbowPos);
      context.convertRealWorldToProjective(rightElbowPos, rightElbowPos);  
      context.convertRealWorldToProjective(leftShoulderPos, leftShoulderPos);
      context.convertRealWorldToProjective(rightShoulderPos, rightShoulderPos);

      context.convertRealWorldToProjective(leftKneePos, leftKneePos);
      context.convertRealWorldToProjective(rightKneePos, rightKneePos);
      context.convertRealWorldToProjective(leftFootPos, leftFootPos);
      context.convertRealWorldToProjective(rightFootPos, rightFootPos);
      context.convertRealWorldToProjective(leftHipPos, leftHipPos);
      context.convertRealWorldToProjective(rightHipPos, rightHipPos);



      stroke(50, 100, 50);
      fill(0, 255, 0);
      ellipse(leftHandPos.x, leftHandPos.y, 20, 20);
      ellipse(rightHandPos.x, rightHandPos.y, 20, 20);
      ellipse(leftHandPos.x, leftHandPos.y, 20, 20);
      ellipse(rightHandPos.x, rightHandPos.y, 20, 20);

      ellipse(leftFootPos.x, leftFootPos.y, 20, 20);
      ellipse(rightFootPos.x, rightFootPos.y, 20, 20);
      ellipse(leftFootPos.x, leftFootPos.y, 20, 20);
      ellipse(rightFootPos.x, rightFootPos.y, 20, 20);

      stroke(0, 0, 255);
      strokeWeight(3);

      context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
      context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
      context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
      context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
      context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
      context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
      context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
      context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
      context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
      context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
    }

    void onNewUser(int userId)
    {
      context.requestCalibrationSkeleton(userId, true);
    }

    void onEndCalibration(int userId, boolean successfull)
    {
      if (successfull)
      {
        context.startTrackingSkeleton(userId);
      }
    }


    class antsWalking {

      float x;
      float y;
      int imageIndex;
      boolean alive = true;     


      antsWalking (float x_, float y_, int imageIndex_) {
        x=x_;
        y=y_;
        imageIndex = imageIndex_;
      }

      void display () {
        image(ants[imageIndex], x, y); //space image
      }

      void move() {
        if (x < width + 50) {
          x+=2;
        }
        else {
          alive = false;
        }
      }
    }

    class Timer
    {
      long startTime ; // time in msecs that timer started
      long timeSoFar ; // use to hold total time of run so far, useful in
      // conjunction with pause and continueRunning
      boolean running ;
      int x, y ; // location of timer output
      Timer(int inX, int inY)
      {
        x = inX ;
        y = inY ;
        running = false ;
        timeSoFar = 0 ;
      }
      int currentTime()
      {
        if ( running )
          return ( (int) ( (millis() - startTime) / 1000.0) ) ;
        else
          return ( (int) (timeSoFar / 1000.0) ) ;
      }
      void start()
      {
        running = true ;
        startTime = millis() ;
      }

      void restart()

      {
        start() ;
      }
      void pause()
      {
        if (running)
        {
          timeSoFar = millis() - startTime ;
          running = false ;
        }

      }
      void continueRunning()

      {
        if (!running)
        {
          startTime = millis() - timeSoFar ;
          running = true ;
        }
      }
      void DisplayTime()
      {
        int theTime ;
        String output = "";
        theTime = currentTime() ;
        output = output + theTime ;
        text(output, 600, 25) ;
      }
    }
Tagged:

Answers

  • Answer ✓

    when clicked, say alive = false

    in move, move it only when alive==true

    in display when alive==false only display the dead ant image

    you're almost there...

    ;-)

  • Almost done!

    I'm using kinect, is there a way when the user does a kick motion it will substitute as a click?

  • since we're in the how to section, why not ask this in the kinect section of this forum?

    I don't know it.

Sign In or Register to comment.