Bug 929 : Sketch must be saved to use constructor
Last modified: 2008-10-17 20:16




Status:
RESOLVED
Resolution:
INVALID -
Priority:
P1
Severity:
normal

 

Reporter:
Tom Blackwell
Assigned To:
REAS

Attachment Type Created Size Actions

Description:   Opened: 2008-09-22 12:22
This is so minor that I hesitate to report it. It occurs when
inserting/changing code in sketches created from the examples menu of the
pde. If you run the sketch without saving it you'll get a message like
"The constructor YourClass(Type) is undefined".
The problem goes away after the sketch is saved.

Found in 0148, running on Windows XP.

To replicate the problem select File|Examples|Basics|Sprite and replace the
example code with that below.

/**
* Rotating Sprite
* by Tom Blackwell.
* Based on sprite example
* by James Patterson.
*
* Demonstrates loading and displaying a transparent GIF image.
*/

PImage teddy;
Sprite sprite;

float xpos;
float ypos;
float rotation = 0;
float drag = 30.0;

void setup()
{
size(200,200, P3D);
teddy = loadImage("teddy.gif");
sprite = new Sprite(teddy);

xpos = width/2;
ypos = height/2;
frameRate(60);
}

void draw()
{
background(102);

float difx = mouseX - xpos-teddy.width/2;
if(abs(difx) > 1.0) {
xpos = xpos + difx/drag;
xpos = constrain(xpos, 0, width-teddy.width);
}

float dify = mouseY - ypos-teddy.height/2;
if(abs(dify) > 1.0) {
ypos = ypos + dify/drag;
ypos = constrain(ypos, 0, height-teddy.height);
}

sprite.draw(xpos, ypos, 0, rotation);
rotation += PI/32;
}

class Sprite {
PImage img;
Float size = 1.0;
Float half_w, half_h;

Sprite(PImage im) {
img = im;
half_w = float(im.width/2); half_h = float(im.height/2);
}

void draw(float x, float y, float z, float angle)
{
Boolean stroke_state = g.stroke;
noStroke();
pushMatrix();
//translate(x+half_w, y+half_h, z);
translate(x, y, z);
rotate(angle);
beginShape();
textureMode(NORMALIZED);
texture(img);
vertex(-half_w, -half_h, 0, 0);
vertex(half_w, -half_h, 1, 0);
vertex(half_w, half_h, 1, 1);
vertex(-half_w, half_h, 0, 1);
endShape();
popMatrix();
g.stroke = stroke_state;
}
}
Additional Comment #1 From fry 2008-09-24 14:13
oh, thanks for reporting--that's actually kind of a major thing since it
means something regressed internally. much appreciated and will fix for 0149.
Additional Comment #2 From fry 2008-10-11 11:55
Ah, on closer look it's actually a different problem--the sketch is called
Sprite (meaning that its class will be named Sprite when compiled), and
there's also a class inside it named "Sprite" as well.

So in fact this is a reference bug, that the Sprite example just needs a
new name so that it's not trying to create two "Sprite" classes and causing
confusion. Hopefully we can get a fixed example in there shortly.
Additional Comment #3 From REAS 2008-10-12 11:30
I don't see the problem in the example. I'm looking at the example Basics >
Image > Sprite and it doesn't have a Sprite class inside it. I think the
Sprite class was added by Tom? Or it's another example.

Even if this is the case, it's clear the issue needs to be mentioned. I'll
add text to the reference and to the first example that uses a Class.
Additional Comment #4 From fry 2008-10-12 18:04
k, then mark the bug as invalid and we'll close it.

the class naming thing is also the last item under "common problems" inside
troubleshooting:
http://processing.org/reference/troubleshooting/index.html#issues