text
In the project above, I’m trying to make it so when I press the ‘c’ key it changes a value. But when I press it again a separate time it restores a value to the original. But no matter what I try, if I press the key to long it will rapidly switch between the new and original value. How does one fix this?
/*
TODO:
MAKE TESTER FOR KEY WENT DOWN
AND CONTROLLER BUTTON WENT DOWN
make collisions
make edges of player/touching testers
gravity/jumping stuff
debug for newObjects
What this program does:
A little platformer test
*/
var world = {
width: 400,
height: 400,
};
var mouse = {
down: false,
};
var deadzone = 0.1;
function setup() {
world.canvas = createCanvas(world.width, world.height);
window.addEventListener("gamepadconnected", function (e) {
gamepadHandler(e);
console.log("Gamepad connected");
});
}
function draw() {
controllers = navigator.getGamepads();
background(220);
player.draw(true);
//player.collides(test);
test.draw();
}
//Base function that rans code that has timing
function run() {
controllers[0] ? testController() : 0;
}
setInterval(function () {
run();
}, 100);
/*
Variables
*/
var test = newObject(200, 200, 100, 20);
var player = {
debug: true,
x: 200,
y: 0,
width: 20,
height: 20,
velocityX: 0,
velocityY: 0,
maxSpeed: 5,
speed: 0.0325,
decell: 0.065,
direction: 0,
format: { color: 0, Ocolor: 0, Osize: 0 },
controlType: "keyboard",
controls: {
keyboard: { left: "a", right: "d", jump: "w" },
controller: { left:"Left Stick Left", right:"Left Stick Right", jump: "a" },
},
gravity: false,
colliders: [],
collider: {
x: self.x,
y: self.y,
width: self.width,
height: self.height,
offSet: [0, 0],
rotation: 0,
},
touching: {
left: false,
right: false,
top: false,
bottom: false,
},
};
//makes player fall
player.fall = function () {
let p = player;
p.velocityY += 0.0025 * (p.width * p.height);
};
//set what objects collide with player
player.collides = function (error) {
error = !error ? 0.5 : error;
//let type = (arguments.length>1)?"list":"single";
let objects = player.colliders;
for (let arg in arguments) {
let object = arguments[arg];
//if argument isnt a list add to objects list
if (!object.length) {
objects.push(object);
break;
}
//if argument is list, add items from there to objects list
if (typeof object.length == "number")
for (let item in object) {
if (!object.length) {
objects.push(object);
break;
}
}
}
for (let obj of objects) {
let collider = obj.collider;
let p = player;
if (!collider) {
let x = obj.x;
let y = obj.y;
let w = obj.width;
let h = obj.height;
obj.collider = {
x: x,
y: y,
width: w,
height: h,
offSet: [0, 0],
rotation: obj.rotation ? obj.rotation : 0,
};
collider = obj.collider;
}
let P = {
c: p.collider,
rx: p.width / 2,
ry: p.height / 2,
xOff: p.collider.offSet[0],
yOff: p.collider.offSet[1],
sides: {
left: p.x - p.width / 2 - p.collider.offSet[0],
right: p.x + p.width / 2 + p.collider.offSet[0],
top: p.y - height / 2 - p.collider.offSet[1],
bottom: p.y + height / 2 + p.collider.offSet[1],
},
};
let O = {
c: obj.collider,
rx: obj.width / 2,
ry: obj.height / 2,
xOff: obj.collider.offSet[0],
yOff: obj.collider.offSet[1],
sides: {
left: obj.x - obj.width / 2 - obj.collider.offSet[0],
right: obj.x + obj.width / 2 + obj.collider.offSet[0],
top: obj.y - obj.height / 2 - obj.collider.offSet[1],
bottom: obj.y + obj.height / 2 + obj.collider.offSet[1],
},
};
/*
collition test is just testing if a side of 'a' object intersects with 'b' object
*/
//set what side is touched
p.touching.left = P.sides.left < O.sides.right + error;
p.touching.right = P.sides.right < O.sides.left + error;
p.touching.top = P.sides.top < O.sides.bottom + error;
p.touching.bottom = P.sides.bottom < O.sides.top + error;
let s = p.touching;
let _s = P.sides;
let _o = O.sides;
switch (true) {
case P.sides.left < O.sides.right:
{
//p.x+=_s.left - _o.right;
console.log("Player touching left!");
}
break;
case P.sides.right < O.sides.left:
{
p.x += _s.right - _o.left;
console.log("Player touching right!");
}
break;
case P.sides.top < O.sides.bottom:
{
p.x += _s.top - _o.bottom;
console.log("Player touching top!");
}
break;
case P.sides.bottom < O.sides.top + error:
{
p.x += _s.bottom - _o.top;
console.log("Player touching bottom!");
}
break;
}
}
};
//allows player to jump and fall
player.jump = function () {
let p = player;
if (p.touching.bottom) {
if (keyDown(p.controls.jump)) {
p.velocityY -= 3;
}
} else {
//p.fall();
}
};
//allows player to move
player.movement = function () {
let p = player;
if (p.controlType == "keyboard") {
let c = player.controls.keyboard;
if (keyDown(c.left) && p.velocityX > -p.maxSpeed) {
p.velocityX -= p.speed;
p.direction = -1;
} else if (!keyDown(c.left) && p.velocityX + p.decell < 0) {
p.velocityX += p.decell;
}
if (keyDown(c.right) && p.velocityX < p.maxSpeed) {
p.velocityX += p.speed;
p.direction = 1;
} else if (!keyDown(c.right) && p.velocityX - p.decell > 0) {
p.velocityX -= p.decell;
}
if (!keyDown(c.left) && p.velocityX > 0 && p.direction < 0) p.velocityX = 0;
else if (!keyDown(c.right) && p.velocityX < 0 && p.direction > 0)
p.velocityX = 0;
}
if(p.controlType=="controller"){
let c = player.controls.controller;
let left = getStick(c.left)<0-deadzone;
let right = getStick(c.right)>0+deadzone;
if (left && p.velocityX > -p.maxSpeed) {
p.velocityX = p.maxSpeed*getStick(c.left);
p.direction = -1;
}
if (right && p.velocityX < p.maxSpeed) {
p.velocityX += p.maxSpeed*getStick(c.right);
p.direction = 1;
}
if (!left && p.velocityX > 0 && p.direction < 0) p.velocityX = 0;
else if (!right && p.velocityX < 0 && p.direction > 0)
p.velocityX = 0;
}
if (p.direction < 0 && p.velocityX < -p.maxSpeed) p.velocityX = -p.maxSpeed;
else if (p.direction > 0 && p.velocityX > p.maxSpeed)
p.velocityX = p.maxSpeed;
};
//updates player values
player.update = function (type) {
type = !type ? "all" : type;
let p = player;
switch (type) {
default:
case "all":
{
p.x += p.velocityX;
p.y += p.velocityY;
p.movement();
p.collides();
p.jump();
}
break;
}
};
//draws and renders player and player methods
player.draw = function (update) {
let _updater = update ? player.update() : false;
let s = player.format;
let Ocolor = s.Ocolor;
let Osize = s.Osize;
let p = player;
let selected = false;
if (p.debug) {
if(keyIsDown){
switch(true){
case keyPressed("c")&&p.controlType=="controller":{
console.log("keyboard Mode!");
p.controlType="keyboard";
}break;
case keyPressed("c")&&p.controlType=="keyboard":{
console.log("controller Mode!");
p.controlType="controller";
}break;
}
}
let overPlayer =
mouseX > player.x - player.width / 2 &&
mouseX < player.x + player.width / 2 &&
mouseY > player.y - player.height / 2 &&
mouseY < player.y + player.height / 2;
text(overPlayer, 150, 150);
if (mouse.down) {
if (overPlayer) {
selected = true;
} else {
selected = false;
}
if (selected) {
p.x = mouseX;
p.y = mouseY + 3;
}
}
//function mouseDragged(){ if ( selected ) { p.x = mouseX; p.y = mouseY; } }
world.canvas.mouseReleased(function () {
selected = false;
Ocolor = s.Ocolor;
Osize = s.Osize;
});
}
rectMode(CENTER);
colorMode(RGB);
fill(s.color);
stroke(!selected ? s.Ocolor : color(0, 255, 0));
strokeWeight(!selected ? s.Osize : 1);
rect(p.x, p.y, p.width, p.height);
if (selected) {
let tw = player.width * 0.2;
let th = player.height * 0.2;
strokeWeight(1.25);
line(player.x - tw, player.y, player.x + tw, player.y);
line(player.x, player.y - th, player.x, player.y + th);
push();
textSize(12);
text(
"Player",
player.x + tw + player.width / 2,
player.y - th - player.height / 2
);
pop();
}
if (p.velocityX > p.maxSpeed || p.velocityX < -p.maxSpeed)
console.log("Movement Error: " + p.velocityX);
};
/*
Functions
*/
//is {k} key down?
function keyDown(k) {
return keyIsPressed && key == k;
}
function keyPressed(k){
return keyDown(k);
}
function newObject(x, y, w, h) {
let o = {
x: x,
y: y,
velocityX: 0,
velocityY: 0,
width: w,
height: h,
format: { color: 0, Ocolor: 0, Osize: 0 },
};
o.update = function (callback) {
o.x += o.velocityX;
o.y += o.VelocityY;
typeof callback == "function" ? callback() : false;
};
o.draw = function () {
let f = o.format;
rectMode(CENTER);
fill(f.color);
stroke(f.Ocolor);
strokeWeight(f.Osize);
rect(o.x, o.y, o.width, o.height);
};
return o;
}
function mousePressed() {
mouse.down = true;
}
function mouseReleased() {
mouse.down = false;
}
function testController() {
if (controllers[0]) {
let controller = controllers[0];
for (let i in controller.buttons) {
let button = controller.buttons[i];
//(buttonPressed(button))?console.log("Pressed: "+controls[i]):0;
}
/*
axes 0: left stick horazontal
//(axes<0-deadzone)?console.log("Right Stick Up!"):0;
//(axes>0+deadzone)?console.log("Right Stick Down!"):0;
*/
for (let a in controller.axes) {
let axes = controller.axes[a];
switch (true) {
case a == 0:
{
}
break;
case a == 1:
{
}
break;
case a == 2:
{
}
break;
case a == 3:
{
}
break;
}
}
}
}
//gets a control from name or index
function getControl(control) {
if (controllers[0]) {
let controller = controllers[0];
if (typeof control != "string") control = _controls[control];
return controller.buttons[controls[control]];
}
}
function inputActvie(control) {
if (controllers[0]) return buttonPressed(getControl(control));
}
//gives axes value of left/right stick direction based on name or index
function getStick(control){
if(controllers[0])return controllers[0].axes[ (typeof control == "string")?sticks[control]:control ];
}
New contributor
Official Squeaky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1