We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Creating a new movable object everytime I click
Page Index Toggle Pages: 1
Creating a new movable object everytime I click (Read 646 times)
Creating a new movable object everytime I click
Jan 14th, 2007, 5:38pm
 
Hi,

I want my program to create a new "card" every time I click on an icon in the left corner.


I made two versions:

http://001.00.funpic.de/processing/

Version 1 is working but only creates one card.

Version 2 does not work. I am getting these errors:
  • Temporary_6057_5044.java:17:8:17:18: Semantic Error: The type of the right sub-expression, "int", is not assignable to the variable, of type "int[]".
  • Temporary_4380_5324.java:25:3:25:22: Semantic Error: The type of the right sub-expression, "processing.core.PImage", is not assignable to the variable, of type "processing.core.PImage[]".

This is the source code of version 2:

PFont schrift;
PFont schrift2;
PImage portrait;
PImage [] iportrait;

int rectx;
int recty;
int numberofcards = 0;

int [] cardx = 160;
int [] cardy = 20;

void setup() {
 size(800,600);
 schrift = loadFont("BitstreamVeraSans-Roman-14.vlw");
 schrift2 = loadFont("BitstreamVeraSans-Bold-24.vlw");
 portrait = loadImage("portrait.jpg");
 iportrait = portrait;
 rectx = width-100;
 recty = height-100;
}

void draw() {
 background(255);
 noFill();
 rect(20,20,111,151);
 image(portrait, 21, 21);
 textFont(schrift2, 24);
 fill(255);
 text("NEW",42,166);
 me();
 card();
}

void me() {
 stroke(0);
 fill(180,0,0);
 rect(rectx,recty,50,50);
 textFont(schrift2, 24);
 fill(255);
 text("ME",rectx+6,recty+35);  
}

void card(){
 if (numberofcards != 0) {
   /*This is a card*/
   stroke(0);
   fill(255);
 
   line(cardx[numberofcards], cardy[numberofcards], cardx[numberofcards], cardy[numberofcards]+150);
   line(cardx[numberofcards], cardy[numberofcards], cardx[numberofcards]+340, cardy[numberofcards]);
   line(cardx[numberofcards]+340, cardy[numberofcards], cardx[numberofcards]+340, cardy[numberofcards]+150);
   line(cardx[numberofcards]+340, cardy[numberofcards]+151, cardx[numberofcards], cardy[numberofcards]+151);

   image(iportrait[numberofcards], cardx[numberofcards]+1, cardy[numberofcards]+1);
   line(cardx[numberofcards]+110, cardy[numberofcards], cardx[numberofcards]+110, cardy[numberofcards]+150);
   textFont(schrift, 14);
   fill(0);
   text("Name:",cardx[numberofcards]+120,cardy[numberofcards]+30);
   text("Tags:",cardx[numberofcards]+120,cardy[numberofcards]+55);
   text("Notes:",cardx[numberofcards]+120,cardy[numberofcards]+80);
 }
}

void mousePressed() {
 /* Create a new card */
 if (mousePressed == (mouseX > 20) && (mouseX < 131) && (mouseY > 20) && (mouseY < 171)) {
 numberofcards++;  
 card();
 }
}

void mouseDragged() {
 
 /* Moving me */
 if (mousePressed == (mouseX > rectx) && (mouseY > recty) && (mouseX < rectx+50) && (mouseY < recty+50))
 {
 rectx = mouseX-25;
 recty = mouseY-25;
 }
 
 /* Moving card */
 if (mousePressed == (mouseX > cardx[numberofcards]) && (mouseY > cardy[numberofcards]) && (mouseX < cardx[numberofcards]+331) && (mouseY < cardy[numberofcards]+151))
 {
 cardx[numberofcards] = mouseX-25;
 cardy[numberofcards] = mouseY-25;
 }
}

Why does the program not work? What do I have to change to get it working?
Re: Creating a new movable object everytime I clic
Reply #1 - Jan 14th, 2007, 7:50pm
 
hi there,

the error says you are trying to assign an object to another object of type array. basically these are handled as different types, so: int is not int[] !

Code:

// _1_
// you set iportrait to be an array of PImages
PImage [] iportrait;

// that's why you can't assign a single object to it
iportrait = portrait;

// instead do:
iportrait = new PImage[]{ portrait };

// this will put portrait at position 0 of iportrait:
image( iportrait[0], 10, 10);

// _2_
// same problem with:
int [] cardx = 160;

// should be:
int [] cardx = new int[]{ 160 }; // put 160 at cardx[0]

// or
int [] cardx = new int[160]; // empty, length 160


have a look here on how to work with arrays:
http://processing.org/learning/examples/array.html

F
Page Index Toggle Pages: 1