March 23rd - point and click
Posted: Thu Mar 24, 2022 12:41 am
point and click demo:
Code: Select all
var pkmaster;
var moving = false;
function preload() {
// put preload code here
pkmaster = new pksprite();
pkmaster.loadSprite();
}
function setup() {
// put setup code here
createCanvas(400,400);
frameRate(30);
}
function draw() {
// put drawing code here
background(255);
pkmaster.draw();
}
class pksprite {
constructor () {
this.direction = 0;
this.sprites = [];
this.spriteCounter = 4;
this.fc = 0;
this.x = 100;
this.y= 100;
}
loadSprite() {
for (var i = 0 ; i < this.spriteCounter ; i++) {
var str = '/assets/down/0'+i+'.png' ;
console.log(str);
this.sprites.push( loadImage(str) );
}
}
draw() {
var speed = 1;
if(moving) {
this.fc++;
if (destX > this.x) {
this.x += speed;
}
if (destX < this.x) {
this.x -= speed;
}
if (destY > this.y) {
this.y += speed;
}
if (destY < this.y) {
this.y -= speed;
}
if(destX == this.x && destY == this.y ) {
moving = false;
}
} else {
this.fc = 0;
}
image(this.sprites[floor(this.fc/2)%this.spriteCounter],this.x,this.y);
}
}
function mouseClicked() {
if(mouseX > width/2) {
destX = width/2 -34;
}
else {
destX = mouseX-17;
}
destY = mouseY-25;
moving = true;
}