Code edit (14 edits merged)
Please save this source code
User prompt
disable buttonA interactions
User prompt
disable buttonA
Code edit (4 edits merged)
Please save this source code
User prompt
when passing in throwing mode, change posture to throwingPosture
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
rename all joystickMode to isThrowingMode
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: joystick.setTexture is not a function' in or related to this line: 'joystick.setTexture('buttonA');' Line Number: 795
User prompt
Fix Joystick Mode changes but not the joystick asset
Code edit (1 edits merged)
Please save this source code
User prompt
console log the joystick mode
User prompt
after a tap gesture, if joystickMode = 1, toggle the asset of the joystick from 'joystick' to 'buttonA'; else restore 'joystick'
User prompt
when joystickMode = 1, dynamically toggle the asset of the joystick from 'joystick' to 'buttonA'
User prompt
when joystickMode = 1, toggle the asset of the joystick from 'joystick' to 'buttonA'
User prompt
only set joystickDrag = true; when after the check that gesture is not a tap
Code edit (1 edits merged)
Please save this source code
User prompt
on Tap gesture, if player has the ball, toggle joystickMode
User prompt
add a joystickMode global variable, it will have 2 values : 0 = Move mode; 1 = Throw mode
User prompt
implement isTapGesture update
User prompt
add global variables to differentiate Tap and Drag gestures
User prompt
As there’s no more “touch to go there”, change targetPoint to isMoving and instead of setting target point with the joystick, set x and y speed
User prompt
There’s a problem is that shoot () isn't called at all when pointing buttonA up and releasing the button
User prompt
There’s a problem not the shoot direction calculation, but it is that shoot () isn't called at all when pointing up and releasing the button
/**** * Classes ****/ // Assets will be automatically created based on usage in the code. // Ball class for the basketball var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('basketball', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 0.5; self.isShot = false; self.currentPlayer = null; // New property to track the current player holding the ball self.shoot = function (power, angle) { self.speedX = Math.cos(angle) * power; self.speedY = Math.sin(angle) * -power; // Negate Y to move up self.isShot = true; }; self.update = function () { if (self.isShot) { self.x += self.speedX; self.y += self.speedY; // Removed gravity effect for isometric view // Boundary checks if (self.x < courtLeftBoundary + self.width / 2) { self.x = courtLeftBoundary + self.width / 2; self.speedX *= -0.5; // Bounce back with reduced speed } if (self.x > courtRightBoundary - self.width / 2) { self.x = courtRightBoundary - self.width / 2; self.speedX *= -0.5; // Bounce back with reduced speed } } }; self.reset = function () { self.x = 1024; // Center X self.y = 2000; // Starting Y self.isShot = false; self.speedY = 0; }; }); // Hoop class for the basketball hoop var Hoop = Container.expand(function () { var self = Container.call(this); var hoopGraphics = self.attachAsset('hoop', { anchorX: 0.5, anchorY: 0.5 }); self.setPosition = function () { self.x = 1024; // Center X self.y = 200; // Hoop Y position }; }); // Player class for the player var Player = Container.expand(function () { var self = Container.call(this); self.hasBall = false; // New property to indicate if the player has the ball self.currentPosture = ""; // New property to store the name of the current posture self.verticalDirection = 0; // 0 when idle or moving down, 1 when moving up self.head = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.8, scaleY: 1 }); self.eyes = self.attachAsset('eyes', { anchorX: 0.5, anchorY: 0.75, scaleX: 0.8, scaleY: 1 }); self.rightArm = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 1, scaleX: 0.5, scaleY: 1.5 }); self.rightHand = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.5 }); self.leftArm = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 1, scaleX: 0.5, scaleY: 1.5 }); self.leftHand = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.5 }); self.trunk = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 2.0 }); self.rightLeg = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 2 }); self.rightFoot = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.5 }); self.leftLeg = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 2 }); self.leftFoot = self.attachAsset('bodyPart', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.5 }); self.updatePosture = function () { var speed = 0.1; // Speed of transition if (!this.targetPosture) { return; } //this.x += (this.targetPosture.x - this.x) * speed; //this.y += (this.targetPosture.y - this.y) * speed; // Check if the target posture for hands and legs is reached var handsAndLegsReached = Math.abs(this.rightHand.x - this.targetPosture.rightHand.x) < 1 && Math.abs(this.rightHand.y - this.targetPosture.rightHand.y) < 1 && Math.abs(this.leftHand.x - this.targetPosture.leftHand.x) < 1 && Math.abs(this.leftHand.y - this.targetPosture.leftHand.y) < 1 && Math.abs(this.rightLeg.x - this.targetPosture.rightLeg.x) < 1 && Math.abs(this.rightLeg.y - this.targetPosture.rightLeg.y) < 1 && Math.abs(this.leftLeg.x - this.targetPosture.leftLeg.x) < 1 && Math.abs(this.leftLeg.y - this.targetPosture.leftLeg.y) < 1; if (handsAndLegsReached) { // Update currentPosture to the name of the targetPosture when hands and legs posture is reached this.currentPosture = this.targetPosture.name; } if (this.targetPosture && this.targetPosture.head) { this.head.x += (this.targetPosture.head.x - this.head.x) * speed; } if (this.targetPosture && this.targetPosture.eyes) { this.eyes.x += (this.targetPosture.eyes.x - this.eyes.x) * speed; this.eyes.y += (this.targetPosture.eyes.y - this.eyes.y) * speed; this.eyes.visible = enableEye && this.verticalDirection === 0; } this.head.y += (this.targetPosture.head.y - this.head.y) * speed; this.rightArm.x += (this.targetPosture.rightArm.x - this.rightArm.x) * speed; this.rightArm.y += (this.targetPosture.rightArm.y - this.rightArm.y) * speed; this.rightHand.x += (this.targetPosture.rightHand.x - this.rightHand.x) * speed; this.rightHand.y += (this.targetPosture.rightHand.y - this.rightHand.y) * speed; this.leftArm.x += (this.targetPosture.leftArm.x - this.leftArm.x) * speed; this.leftArm.y += (this.targetPosture.leftArm.y - this.leftArm.y) * speed; this.leftHand.x += (this.targetPosture.leftHand.x - this.leftHand.x) * speed; this.leftHand.y += (this.targetPosture.leftHand.y - this.leftHand.y) * speed; this.trunk.x += (this.targetPosture.trunk.x - this.trunk.x) * speed; this.trunk.y += (this.targetPosture.trunk.y - this.trunk.y) * speed; this.rightLeg.x += (this.targetPosture.rightLeg.x - this.rightLeg.x) * speed; this.rightLeg.y += (this.targetPosture.rightLeg.y - this.rightLeg.y) * speed; this.rightFoot.x += (this.targetPosture.rightFoot.x - this.rightFoot.x) * speed; this.rightFoot.y += (this.targetPosture.rightFoot.y - this.rightFoot.y) * speed; this.leftLeg.x += (this.targetPosture.leftLeg.x - this.leftLeg.x) * speed; this.leftLeg.y += (this.targetPosture.leftLeg.y - this.leftLeg.y) * speed; this.leftFoot.x += (this.targetPosture.leftFoot.x - this.leftFoot.x) * speed; this.leftFoot.y += (this.targetPosture.leftFoot.y - this.leftFoot.y) * speed; }; self.updatePosition = function () { self.isMoving = false; self.speedX = 0; self.speedY = 0; if (joystickDrag) { self.isMoving = true; var dx = joystick.x - joystickBasePosition.x; var dy = joystick.y - joystickBasePosition.y; self.speedX = dx * 0.1; // Adjust speed factor as needed self.speedY = dy * 0.1; // Adjust speed factor as needed } // Apply speed self.x += self.speedX; self.y += self.speedY; // Boundary checks if (self.x < courtLeftBoundary + self.width / 2) { self.x = courtLeftBoundary + self.width / 2; } else if (self.x > courtRightBoundary - self.width / 2) { self.x = courtRightBoundary - self.width / 2; } if (self.y < courtTopBoundary + self.height / 2) { self.y = courtTopBoundary + self.height / 2; } else if (self.y > courtBottomBoundary - 500 - self.height / 2) { self.y = courtBottomBoundary - 500 - self.height / 2; } // Posture updates based on movement if (self.isMoving) { var currentTick = LK.ticks; if (currentTick % 20 < 10) { self.setPosture(self.hasBall ? runningUpWithBall1 : runningUp1); } else { self.setPosture(self.hasBall ? runningUpWithBall2 : runningUp2); } } else { if (self.hasBall) { var currentTick = LK.ticks; self.setPosture(Math.abs(ball.y - self.y) > 150 ? dribblingPosture1 : dribblingPosture2); } else { self.setPosture(idlePosture); } } }; self.setPosture = function (newPosture) { console.log("Entering setPosture " + self.currentPosture + " => " + newPosture.name); if (self.currentPosture != newPosture.name) { console.log("Changing posture"); self.targetPosture = newPosture; } else { console.log("Already in posture"); } }; self.goToPoint = function (newPosition) { self.targetPoint = newPosition; }; }); /**** * Initialize Game ****/ // Player positions var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Define court boundaries as global variables var courtTopBoundary = 0; var courtBottomBoundary = 2732; var courtLeftBoundary = 0; var courtRightBoundary = 2048; // Removed simulateAnchorY1 function and its usage var buttonA = game.addChild(LK.getAsset('buttonA', { anchorX: 0.5, anchorY: 0.5, alpha: 0.85, x: 2048 - 300, y: 2732 - 250 })); // Create and position the joystick var joystick = game.addChild(LK.getAsset('joystick', { anchorX: 0.5, anchorY: 0.5, alpha: 0.75, x: 300, // Positioned on the left with some margin // Center X y: 2732 - 250 // Positioned at the bottom with some margin })); /******* Retro Basket ******** A retro basketball game. It's a one vs one basketball game. Ball is lanched in the middle. Players must run and catch it, then throw it in the oppoent hoop. *****************************/ var idlePosture = { name: "idlePosture", x: 1024, y: 2000, head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 110, y: 50 }, rightHand: { x: 125, y: 30 }, leftArm: { x: -110, y: 50 }, leftHand: { x: -120, y: 30 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 37.5, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -37.5, y: 150 }, leftFoot: { x: -50, y: 250 } }; var dribblingPosture1 = { name: "dribblingPosture1", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 90, y: 50 // Adjusted from -20 to 50 }, rightHand: { x: -90, y: 30 }, leftArm: { x: -90, y: 50 // Adjusted from -20 to 50 }, leftHand: { x: 90, y: 30 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -40, y: 150 }, leftFoot: { x: -50, y: 250 } }; var dribblingPosture2 = { name: "dribblingPosture2", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 90, y: 50 // Adjusted from -20 to 50 }, rightHand: { x: -90, y: 50 }, leftArm: { x: -90, y: 50 // Adjusted from -20 to 50 }, leftHand: { x: 90, y: 50 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -40, y: 150 }, leftFoot: { x: -50, y: 250 } }; var runningUp1 = { name: "runningUp1", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 90, y: 50 // Adjusted from -20 to 50 by adding 70 }, rightHand: { x: 105, y: 30 }, leftArm: { x: -90, y: 50 }, leftHand: { x: -105, y: 30 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 100 }, rightFoot: { x: 50, y: 200 }, leftLeg: { x: -40, y: 150 }, leftFoot: { x: -50, y: 250 } }; var runningUp2 = { name: "runningUp2", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 80, y: 50 // Adjusted from -20 to 50 by adding 70 }, rightHand: { x: 95, y: 30 }, leftArm: { x: -80, y: 50 }, leftHand: { x: -95, y: 30 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -40, y: 100 }, leftFoot: { x: -50, y: 200 } }; var runningUpWithBall1 = { name: "runningUpWithBall1", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 90, y: 20 // Adjusted from -20 to 50 by adding 70 }, rightHand: { x: -0, y: -20 }, leftArm: { x: -90, y: 20 }, leftHand: { x: 0, y: -20 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 100 }, rightFoot: { x: 50, y: 200 }, leftLeg: { x: -40, y: 150 }, leftFoot: { x: -50, y: 250 } }; var runningUpWithBall2 = { name: "runningUpWithBall2", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 80, y: -20 // Adjusted from -90 to -20 by adding 70 }, rightHand: { x: 5, y: 15 }, leftArm: { x: -80, y: -20 }, leftHand: { x: 5, y: 15 }, trunk: { x: 0, y: 0 }, rightLeg: { x: 40, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -40, y: 100 }, leftFoot: { x: -50, y: 200 } }; var throwingPosture = { name: "throwingPosture", x: 1024, // Center X y: 2000, // Player Y position head: { x: 0, y: -160 }, eyes: { x: 0, y: -160 }, rightArm: { x: 100, y: -120 }, rightHand: { x: 100, y: -250 // Set the right hand at the top of the right arm }, leftArm: { x: -100, y: -120 }, leftHand: { x: -100, y: -250 // Set the left hand at the top of the left arm }, trunk: { x: 0, y: 0 }, rightLeg: { x: 37.5, y: 150 }, rightFoot: { x: 50, y: 250 }, leftLeg: { x: -37.5, y: 150 }, leftFoot: { x: -50, y: 250 } }; var player = game.addChild(new Player()); player.x = 2048 / 2; // Center X player.y = 2732 / 2; // Center Y player.setPosture(idlePosture); var ball = game.addChild(new Ball()); ball.reset(); var hoop = game.addChild(new Hoop()); hoop.setPosition(); var score = 0; var scoreTxt = new Text2("0" + score.toString(), { size: 150, fill: "#ffffff" }); scoreTxt.x = -180; scoreTxt.y = -10; var enableEye = true; LK.gui.topRight.addChild(scoreTxt); var joystickBasePosition = { x: joystick.x, y: joystick.y }; var buttonABasePosition = { x: buttonA.x, y: buttonA.y }; var joystickDrag = false; var buttonADrag = false; game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); // Calculate distance between click and joystick center var distance = Math.sqrt(Math.pow(pos.x - joystick.x, 2) + Math.pow(pos.y - joystick.y, 2)); // Calculate distance between click and buttonA center var distanceToButtonA = Math.sqrt(Math.pow(pos.x - buttonA.x, 2) + Math.pow(pos.y - buttonA.y, 2)); // If click is within joystick radius, start dragging if (distance <= joystick.width / 2) { joystickDrag = true; } else if (distanceToButtonA <= buttonA.width / 2) { buttonADrag = true; } else { // If click is outside joystick radius and not within buttonA radius, treat it as a target position //player.goToPoint({ // x: pos.x, // y: pos.y //}); } }); game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); if (joystickDrag) { var dx = pos.x - joystickBasePosition.x; var dy = pos.y - joystickBasePosition.y; var distance = Math.sqrt(dx * dx + dy * dy); var maxDistance = joystick.width / 4; // Max distance joystick can move from center if (distance > maxDistance) { var angle = Math.atan2(dy, dx); dx = Math.cos(angle) * maxDistance; dy = Math.sin(angle) * maxDistance; } joystick.x = joystickBasePosition.x + dx; joystick.y = joystickBasePosition.y + dy; // Update player target point based on joystick movement player.goToPoint({ x: player.x + dx * 5, y: player.y + dy * 5 }); } else if (buttonADrag) { if (!player.hasBall) { player.setPosture(throwingPosture); } var dxButtonA = pos.x - (2048 - 300); var dyButtonA = pos.y - (2732 - 250); var distanceButtonA = Math.sqrt(dxButtonA * dxButtonA + dyButtonA * dyButtonA); var maxDistanceButtonA = buttonA.width / 4; // Max distance buttonA can move from center if (distanceButtonA > maxDistanceButtonA) { var angleButtonA = Math.atan2(dyButtonA, dxButtonA); dxButtonA = Math.cos(angleButtonA) * maxDistanceButtonA; dyButtonA = Math.sin(angleButtonA) * maxDistanceButtonA; } buttonA.x = 2048 - 300 + dxButtonA; buttonA.y = 2732 - 250 + dyButtonA; if (player.currentPosture != "throwingPosture") { return; } // Calculate arm rotation based on buttonA movement var armRotationFactor = 0.05; // Factor to control the rotation sensitivity var armRotation = Math.max(-Math.PI / 1.8, Math.min(Math.PI / 1.8, dxButtonA * armRotationFactor)); // Clamp rotation between -100 and 100 degrees converted to radians // Log armRotation in degrees //console.log("Arm rotation: " + (armRotation * (180 / Math.PI)).toFixed(2) + " degrees"); player.rightArm.rotation = armRotation; player.leftArm.rotation = armRotation; // Calculate the top position of the arms and move hands to follow the arms' movement var rightArmTopX = player.rightArm.x + Math.cos(player.rightArm.rotation - Math.PI / 2) * (player.rightArm.height + player.rightHand.height / 1.5); var rightArmTopY = player.rightArm.y + Math.sin(player.rightArm.rotation - Math.PI / 2) * (player.rightArm.height + player.rightHand.height / 1.5); var leftArmTopX = player.leftArm.x + Math.cos(player.leftArm.rotation - Math.PI / 2) * (player.leftArm.height + player.rightHand.height / 1.5); var leftArmTopY = player.leftArm.y + Math.sin(player.leftArm.rotation - Math.PI / 2) * (player.leftArm.height + player.rightHand.height / 1.5); //console.log("rightArmTopX: " + rightArmTopX.toFixed(2) + " y " + rightArmTopY.toFixed(2)); player.rightHand.x = rightArmTopX; player.rightHand.y = rightArmTopY; player.leftHand.x = leftArmTopX; player.leftHand.y = leftArmTopY; } }); game.on('up', function (obj) { if (joystickDrag) { joystickDrag = false; joystick.x = joystickBasePosition.x; joystick.y = joystickBasePosition.y; } if (buttonADrag) { buttonADrag = false; // If the player has the ball, throw it in the direction selected if (player.hasBall) { var throwPower = 20; // Adjust throw power as needed var dx = buttonA.x - buttonABasePosition.x; var dy = -(buttonA.y - buttonABasePosition.y); // Invert Y-axis for correct angle calculation var throwAngle = Math.atan2(dy, dx); console.log("Throw angle in degrees: " + (throwAngle * (180 / Math.PI)).toFixed(2)); // Log throwAngle in degrees ball.shoot(throwPower, throwAngle); player.hasBall = false; // Player no longer has the ball ball.currentPlayer = null; // Clear the ball's currentPlayer property } else { // Restore hands to normal positions after dragging buttonA player.rightArm.rotation = 0; player.leftArm.rotation = 0; // Return player to idle posture player.setPosture(idlePosture); } // Reset buttonA to its original position buttonA.x = 2048 - 300; buttonA.y = 2732 - 250; } }); var idle = true; // Test postures /* var changePostureTimer = LK.setInterval(function () { if (idle) { player.setPosture(runningUp1); idle = false; } else { player.setPosture(runningUp2); idle = true; } }, 1000); */ LK.on('tick', function () { ball.update(); // Check if player's trunk intersects with the ball if (player.trunk.intersects(ball)) { ball.currentPlayer = player; // Assign the player to the ball's currentPlayer property player.hasBall = true; // Assign the player to the ball's currentPlayer property } // Make the ball follow the currentPlayer's position with dribble movement if (ball.currentPlayer) { ball.x = ball.currentPlayer.x; if (ball.currentPlayer.verticalDirection === 0) { ball.y = ball.currentPlayer.y + 150 + Math.sin(LK.ticks / (ball.currentPlayer.targetPoint ? 4 : 6)) * 100; game.addChild(ball); // Ensure ball is in front by re-adding it to the game } else { ball.y = ball.currentPlayer.y - 120 + Math.sin(LK.ticks / (ball.currentPlayer.targetPoint ? 4 : 6)) * 50; game.addChild(ball.currentPlayer); // Ensure player is in front by re-adding it to the game } } if (!buttonADrag || player.currentPosture != "throwingPosture") { player.updatePosture(); player.updatePosition(); } // Re-add joystick to ensure it appears above other elements game.addChild(joystick); // Check if ball intersects with hoop and is moving downwards if (ball.intersects(hoop) && ball.speedY > 0) { score += 1; scoreTxt.setText(score.toString()); ball.reset(); } // Reset ball if it goes off-screen // Check Y boundaries for the ball if (ball.y < courtTopBoundary + ball.height / 2) { ball.y = courtTopBoundary + ball.height / 2; ball.speedY *= -0.5; // Bounce back with reduced speed } if (ball.y > courtBottomBoundary - ball.height / 2) { ball.y = courtBottomBoundary - ball.height / 2; ball.speedY *= -0.5; // Bounce back with reduced speed } });
===================================================================
--- original.js
+++ change.js
@@ -166,66 +166,46 @@
this.leftFoot.x += (this.targetPosture.leftFoot.x - this.leftFoot.x) * speed;
this.leftFoot.y += (this.targetPosture.leftFoot.y - this.leftFoot.y) * speed;
};
self.updatePosition = function () {
- if (self.targetPoint) {
- self.verticalDirection = self.targetPoint.y < self.y ? 1 : 0;
- var fixedSpeed = 10; // Fixed pixels per frame
- // Calculate direction vector
- var dx = self.targetPoint.x - self.x;
- var dy = self.targetPoint.y - self.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- var nx = dx / distance; // Normalized direction x
- var ny = dy / distance; // Normalized direction y
- // Move player by fixed speed in the direction of the targetPoint
- // Move player by fixed speed in the direction of the targetPoint independently for x and y
- self.x += nx * fixedSpeed;
- // Left boundary check
- if (self.x < courtLeftBoundary + self.width / 2) {
- self.x = courtLeftBoundary + self.width / 2;
- }
- // Right boundary check
- else if (self.x > courtRightBoundary - self.width / 2) {
- self.x = courtRightBoundary - self.width / 2;
- }
- self.y += ny * fixedSpeed;
- if (self.y < courtTopBoundary + self.height / 2) {
- self.y = courtTopBoundary + self.height / 2;
- } else if (self.y > courtBottomBoundary - 500 - self.height / 2) {
- self.y = courtBottomBoundary - 500 - self.height / 2;
- }
- // Check if player is close enough to targetPoint to stop
- if (distance < fixedSpeed) {
- if (self.x < courtLeftBoundary + self.width / 2) {
- self.x = courtLeftBoundary + self.width / 2;
- } else if (self.x > courtRightBoundary - self.width / 2) {
- self.x = courtRightBoundary - self.width / 2;
- }
- if (self.y > courtBottomBoundary - 500 - self.height / 2) {
- self.y = courtBottomBoundary - 500 - self.height / 2;
- }
- self.targetPoint = null; // Clear targetPoint when close enough
- self.verticalDirection = 0;
- // Alternate between dribblingPosture1 and dribblingPosture2 when player hasBall and is not moving
- if (self.hasBall && !self.targetPoint) {
- var currentTick = LK.ticks;
- self.setPosture(dribblingPosture1);
- } else {
- self.setPosture(idlePosture);
- }
+ self.isMoving = false;
+ self.speedX = 0;
+ self.speedY = 0;
+ if (joystickDrag) {
+ self.isMoving = true;
+ var dx = joystick.x - joystickBasePosition.x;
+ var dy = joystick.y - joystickBasePosition.y;
+ self.speedX = dx * 0.1; // Adjust speed factor as needed
+ self.speedY = dy * 0.1; // Adjust speed factor as needed
+ }
+ // Apply speed
+ self.x += self.speedX;
+ self.y += self.speedY;
+ // Boundary checks
+ if (self.x < courtLeftBoundary + self.width / 2) {
+ self.x = courtLeftBoundary + self.width / 2;
+ } else if (self.x > courtRightBoundary - self.width / 2) {
+ self.x = courtRightBoundary - self.width / 2;
+ }
+ if (self.y < courtTopBoundary + self.height / 2) {
+ self.y = courtTopBoundary + self.height / 2;
+ } else if (self.y > courtBottomBoundary - 500 - self.height / 2) {
+ self.y = courtBottomBoundary - 500 - self.height / 2;
+ }
+ // Posture updates based on movement
+ if (self.isMoving) {
+ var currentTick = LK.ticks;
+ if (currentTick % 20 < 10) {
+ self.setPosture(self.hasBall ? runningUpWithBall1 : runningUp1);
} else {
- // Alternate between runningUp1 and runningUp2 postures while moving
- var currentTick = LK.ticks;
- if (currentTick % 20 < 10) {
- self.setPosture(self.hasBall ? runningUpWithBall1 : runningUp1);
- } else {
- self.setPosture(self.hasBall ? runningUpWithBall2 : runningUp2);
- }
+ self.setPosture(self.hasBall ? runningUpWithBall2 : runningUp2);
}
} else {
if (self.hasBall) {
var currentTick = LK.ticks;
self.setPosture(Math.abs(ball.y - self.y) > 150 ? dribblingPosture1 : dribblingPosture2);
+ } else {
+ self.setPosture(idlePosture);
}
}
};
self.setPosture = function (newPosture) {
@@ -798,20 +778,17 @@
}
if (buttonADrag) {
buttonADrag = false;
// If the player has the ball, throw it in the direction selected
- if (buttonADrag) {
- if (player.hasBall) {
- var throwPower = 20; // Adjust throw power as needed
- var dx = buttonA.x - buttonABasePosition.x;
- var dy = -(buttonA.y - buttonABasePosition.y); // Invert Y-axis for correct angle calculation
- var throwAngle = Math.atan2(dy, dx);
- console.log("Throw angle in degrees: " + (throwAngle * (180 / Math.PI)).toFixed(2)); // Log throwAngle in degrees
- ball.shoot(throwPower, throwAngle);
- player.hasBall = false; // Player no longer has the ball
- ball.currentPlayer = null; // Clear the ball's currentPlayer property
- }
- buttonADrag = false;
+ if (player.hasBall) {
+ var throwPower = 20; // Adjust throw power as needed
+ var dx = buttonA.x - buttonABasePosition.x;
+ var dy = -(buttonA.y - buttonABasePosition.y); // Invert Y-axis for correct angle calculation
+ var throwAngle = Math.atan2(dy, dx);
+ console.log("Throw angle in degrees: " + (throwAngle * (180 / Math.PI)).toFixed(2)); // Log throwAngle in degrees
+ ball.shoot(throwPower, throwAngle);
+ player.hasBall = false; // Player no longer has the ball
+ ball.currentPlayer = null; // Clear the ball's currentPlayer property
} else {
// Restore hands to normal positions after dragging buttonA
player.rightArm.rotation = 0;
player.leftArm.rotation = 0;