That should work if xpos, ypos and ts are declared outside of draw() and setup(). Variables are declared when you first define them, saying what type they are (int, float, etc) and perhaps giving them a value.
e.g.
Code:
// declare three ints:
int xpos = 0;
int ypos = 0;
int ts = 10;
void setup() {
// you can modify xpos, ypos and ts
// in here because they were declared
// outside of setup() and draw()
}
void draw() {
// same here
}
If you declared them in setup, they won't be accessible in draw:
Code:
void setup() {
int xpos = 0;
int ypos = 0;
int ts = 10;
}
void draw() {
// you can not modify xpos, ypos and ts
// in here because they were declared
// inside setup()
xpos = 10; // this is an error
}
In general, things are accessible (
in scope) if they were declared inside the same pair of curly brackets[ (a
block). Blocks can be
nested (like your if statement - statements inside it are also inside of draw).
Whilst I'm here, it's worth pointing out that it's better to declare your img object outside of draw and setup, and load it in setup, because otherwise it will be loaded every frame. That could be very inefficient.
Hope that helps.