User prompt
in gard(), simplify the adjustments as they are symetrical. use an array for elements
User prompt
now in guard(), add adjustments for arms too
User prompt
now refactor the gard() function
User prompt
in guar(), use the deltas in the else block
Code edit (18 edits merged)
Please save this source code
User prompt
simplify guard() function bt defining local variables for the deltas (like handXDelta, handRDelta, forearmXDelta, ....)
Code edit (1 edits merged)
Please save this source code
User prompt
in guard() function, move the hands ,forearms and hands closer to the center of the torso then restore them when leaving guard state the system for guard & guard button: set & keep athlete in guard state when button is down, restore when button is up
User prompt
change the system for guard & guard button: keep athlete in guard state when button is down, restore when button is up
User prompt
in guard() funciton, move the hands ,forearms and hands closer to the center of the torso then restore them when button is released
User prompt
in guard() funciton, move the hands ,forearms and hands closer to the center of the torso
Code edit (19 edits merged)
Please save this source code
User prompt
implement the athlete guard action
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
use the Ring class in gameInitialize
User prompt
create a Class for the ring
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: player is undefined' in or related to this line: 'if ((player.leftHand.intersects(opponent.head) || player.rightHand.intersects(opponent.head)) && player.isPunching) {' Line Number: 293
Code edit (1 edits merged)
Please save this source code
User prompt
only flash on hit when an athlete is Punching
User prompt
restore self.isPunching
User prompt
update self.isPunching
User prompt
add a propoery isPunching to athlete class
/**** * Classes ****/ /****************************************************************************************** */ /************************************* ATHLETE CLASS ************************************** */ /****************************************************************************************** */ var Athlete = Container.expand(function (isPlayer) { var self = Container.call(this); self.isPunching = false; // Indicates if the athlete is currently punching self.isPlayer = isPlayer; self.body = new Container(); game.addChild(self.body); // Define punch distance for punching animation var punchDistance = 50; // Left Arm self.leftForearm = self.body.attachAsset('forearm', { anchorX: 0.5, anchorY: 0.5, x: -110, y: -125, width: 80, rotation: -Math.PI * 0.2 }); self.leftHand = self.body.attachAsset(isPlayer ? 'hand' : 'hand2', { anchorX: 0.5, anchorY: 0.5, x: isPlayer ? -110 : -120, y: isPlayer ? -250 : -245, width: 80, height: 180, rotation: isPlayer ? -Math.PI * 0.05 : -Math.PI * 0.17 }); self.leftArm = self.body.attachAsset('arm', { anchorX: 0.5, anchorY: 0, x: -120, y: -120 }); // Right Arm self.rightForearm = self.body.attachAsset('forearm', { anchorX: 0.5, anchorY: 0.5, x: 110, y: -125, scaleX: -1, width: 80, rotation: Math.PI * 0.2 }); self.rightHand = self.body.attachAsset(isPlayer ? 'hand' : 'hand2', { anchorX: 0.5, anchorY: 0.5, x: isPlayer ? 110 : 120, y: isPlayer ? -250 : -245, scaleX: -1, width: 80, height: 180, rotation: isPlayer ? Math.PI * 0.05 : Math.PI * 0.17 }); self.rightArm = self.body.attachAsset('arm', { anchorX: 0.5, anchorY: 0, x: 120, y: -120, scaleX: -1 }); self.torso = self.body.attachAsset('body', { anchorX: 0.5, anchorY: 0.5 }); self.head = self.body.attachAsset(isPlayer ? 'head' : 'head2', { anchorX: 0.5, anchorY: 0.5, y: -40 }); // Player movement speed self.speed = 5; // Punch function to handle punching action self.punch = function (isLeft) { if (self.isPunching) { return; } var arm = isLeft ? self.leftArm : self.rightArm; var forearm = isLeft ? self.leftForearm : self.rightForearm; var hand = isLeft ? self.leftHand : self.rightHand; // Punch animation logic self.isPunching = true; LK.setTimeout(function () { arm.height += punchDistance; arm.y -= punchDistance; forearm.height += punchDistance; forearm.y -= punchDistance * 1.5; // Move forearm up for punch forearm.rotation *= 0.5; //= -Math.PI * 0.1; //-Math.PI * 0.2 hand.x += isLeft ? 15 : -15; ; // Move hand up for punch hand.y -= punchDistance * 2; // Move hand up for punch // Add head movement to mimic boxer's dodge self.head.x += isLeft ? 15 : -15; // Move head opposite to punching arm self.head.width *= 0.95; // Simulate head tilt by narrowing the width self.head.height *= 1.05; // Reset head width to simulate head tilt back self.head.rotation -= isLeft ? -0.1 : 0.1; // Simulate head tilt by narrowing the width }, 100); LK.setTimeout(function () { arm.height -= punchDistance; arm.y += punchDistance; forearm.height -= punchDistance; forearm.y += punchDistance * 1.5; // Move forearm up for punch forearm.rotation *= 2; //-Math.PI * 0.2 hand.x += isLeft ? -15 : 15; ; // Move hand up for punch hand.y += punchDistance * 2; // Move hand up for punch self.head.x += isLeft ? -15 : 15; // Move head opposite to punching arm self.head.width /= 0.95; // Reset head width to simulate head tilt back self.head.height /= 1.05; // Reset head width to simulate head tilt back self.head.rotation += isLeft ? -0.1 : 0.1; // Simulate head tilt by narrowing the width self.isPunching = false; }, 300); }; // Guard function to handle guarding action self.guard = function (setGuard) { // Simplified adjustments for guarding using symmetrical elements var adjustPosition = function adjustPosition(elementPair, deltaX, deltaR, setGuard) { elementPair.forEach(function (element, index) { var adjustment = index === 0 ? deltaX : -deltaX; // Invert deltaX for the second element element.x += setGuard ? adjustment : -adjustment; element.rotation += setGuard ? deltaR : -deltaR; }); }; // Pair elements for symmetrical adjustments var elementPairs = [[self.leftHand, self.rightHand], [self.leftForearm, self.rightForearm], [self.leftArm, self.rightArm]]; var adjustments = [{ deltaX: 60, deltaR: 0.2 }, { deltaX: 30, deltaR: 0.2 }, { deltaX: 15, deltaR: 0.1 }]; adjustments.forEach(function (adj, index) { return adjustPosition(elementPairs[index], adj.deltaX, adj.deltaR, setGuard); }); self.isGuarding = setGuard; }; // Update player position based on input self.update = function () { if (false && isPlaying) { // TEMP DEBUG !! // Only move player if it's human controlled automatically self.mainMove(); } // Player is idle self.miniMove(); // Update body position and rotation self.body.x = self.x; self.body.y = self.y; // Calculate angle to face the opponent var target = self.isPlayer ? opponent : player; // Determine target based on whether self is Player or Opponent var angleToOpponent = Math.atan2(target.y - self.y, target.x - self.x) + Math.PI * 0.5; self.body.rotation = angleToOpponent; }; self.targetX = self.targetX || 1024; // Default target X self.targetY = self.targetY || 1366; // Default target Y self.mainMove = function () { // Check if athlete has reached the target var distanceToTarget = Math.sqrt(Math.pow(self.targetX - self.x, 2) + Math.pow(self.targetY - self.y, 2)); if (distanceToTarget < 10) { // Define a new target within the ring var ringCenterX = 1024; // Center of the ring X var ringCenterY = 1366; // Center of the ring Y var movementRadius = Math.random() * 300 + 100; // Random radius within which the athlete can move var angle = Math.random() * Math.PI * 2; // Generate a random angle for movement direction self.targetX = ringCenterX + Math.cos(angle) * movementRadius; self.targetY = ringCenterY + Math.sin(angle) * movementRadius; } else { // Progressively move to the target var moveX = (self.targetX - self.x) / distanceToTarget * self.speed; var moveY = (self.targetY - self.y) / distanceToTarget * self.speed; self.x += moveX; self.y += moveY; } }; self.miniMove = function () { if (self.isPunching || self.isGuarding) { return; } // Mini head movement with added randomness self.head.y = -40 + Math.sin(LK.ticks / 10 + Math.random() * 0.2) * 2; // Mini arm and forearm movement with added randomness self.leftArm.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; self.leftForearm.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add forearm movement self.leftHand.x += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.5; // Add hand movement self.leftHand.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add hand movement self.rightArm.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; self.rightForearm.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add forearm movement self.rightHand.x -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.5; // Add hand movement self.rightHand.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add hand movement }; }); /****************************************************************************************** */ /************************************* GUARD BUTTON CLASS ********************************** */ /****************************************************************************************** */ var GuardButton = Container.expand(function () { var self = Container.call(this); self.buttonAsset = self.attachAsset('guardButton', { anchorX: 0.5, anchorY: 0.5, alpha: 0.8 }); self.x = 400; // Position button in the bottom center self.y = game.height - 350; self.down = function (x, y, obj) { player.guard(true); // Activate guard state for player self.width *= 0.9; self.height *= 0.9; }; self.up = function (x, y, obj) { player.guard(false); self.width /= 0.9; self.height /= 0.9; }; }); /****************************************************************************************** */ /************************************* PUNCH BUTTON CLASS ********************************** */ /****************************************************************************************** */ var PunchButton = Container.expand(function () { var self = Container.call(this); self.buttonAsset = self.attachAsset('punchButton', { anchorX: 0.5, anchorY: 0.5, alpha: 0.8 }); self.x = game.width - 400; // Position button on the bottom right self.y = game.height - 350; self.down = function (x, y, obj) { // Simulate button press by triggering punch with left arm player.punch(Math.random() < 0.5); // Randomly choose left or right arm for punching self.width *= 0.9; self.height *= 0.9; }; self.up = function (x, y, obj) { // Simulate button release by triggering punch with right arm self.width /= 0.9; self.height /= 0.9; }; }); /****************************************************************************************** */ /**************************************** RING CLASS ************************************** */ /****************************************************************************************** */ var Ring = Container.expand(function () { var self = Container.call(this); self.ringAsset = self.attachAsset('ring', { anchorX: 0.5, anchorY: 0.5 }); self.x = 1024; // Center of the screen horizontally self.y = 1366 - self.height / 6; // Center of the screen vertically }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ /****************************************************************************************** */ /************************************** GLOBAL VARIABLES ********************************** */ /****************************************************************************************** */ var isPlaying = false; var ring; var punchButton; var guardButton; var player; var opponent; var touchPosition = null; var swipeStart = null; var swipeEnd = null; var isDebug = false; /****************************************************************************************** */ /*********************************** UTILITY FUNCTIONS ************************************ */ /****************************************************************************************** */ function log() { if (isDebug) { var _console; (_console = console).log.apply(_console, arguments); } } /****************************************************************************************** */ /************************************** INPUT HANDLERS ************************************ */ /****************************************************************************************** */ game.down = function (x, y, obj) { if (!isPlaying) { isPlaying = true; } }; game.move = function (x, y, obj) {}; game.up = function (x, y, obj) {}; /****************************************************************************************** */ /************************************ GAME INITIALIZE ************************************* */ /****************************************************************************************** */ function gameInitialize() { log("Game initialize..."); // Initialize Ring ring = game.addChild(new Ring()); punchButton = game.addChild(new PunchButton()); guardButton = game.addChild(new GuardButton()); // Initialize Player player = game.addChild(new Athlete(true)); player.x = 1024; // Center horizontally player.y = game.height * 0.55; // Position towards the bottom // Initialize Opponent opponent = game.addChild(new Athlete()); opponent.x = 1024; // Center horizontally opponent.y = game.height * 0.2; // Center vertically in the middle of the ring opponent.rotation = Math.PI * 0.5; } /****************************************************************************************** */ /************************************** MAIN GAME LOOP ************************************ */ /****************************************************************************************** */ // Game update function game.update = function () { // This section has been removed to prevent redundant player movement handling. // Check for collision between player's hand and opponent's head if (player.isPunching && (player.leftHand.intersects(opponent.torso) || player.rightHand.intersects(opponent.torso))) { // Player hit reaction: head goes back, flash screen red // Calculate opponent's current direction and move back accordingly var opponentDirection = Math.atan2(opponent.y - player.y, opponent.x - player.x); opponent.x += Math.cos(opponentDirection) * 50; // Move opponent back in the x direction of current movement opponent.y += Math.sin(opponentDirection) * 50; // Move opponent back in the y direction of current movement opponent.head.y -= 20; // Head returns to normal position LK.effects.flashScreen(0xFFFFFF, 100); // Flash the whole screen red for 0.5 seconds LK.setTimeout(function () { opponent.head.y += 20; // Head returns to normal position }, 500); } }; gameInitialize();
===================================================================
--- original.js
+++ change.js
@@ -116,40 +116,30 @@
}, 300);
};
// Guard function to handle guarding action
self.guard = function (setGuard) {
- var adjustPosition = function adjustPosition(element, deltaX, deltaR, setGuard) {
- element.x += setGuard ? deltaX : -deltaX;
- element.rotation += setGuard ? deltaR : -deltaR;
+ // Simplified adjustments for guarding using symmetrical elements
+ var adjustPosition = function adjustPosition(elementPair, deltaX, deltaR, setGuard) {
+ elementPair.forEach(function (element, index) {
+ var adjustment = index === 0 ? deltaX : -deltaX; // Invert deltaX for the second element
+ element.x += setGuard ? adjustment : -adjustment;
+ element.rotation += setGuard ? deltaR : -deltaR;
+ });
};
- // Adjustments for guarding
+ // Pair elements for symmetrical adjustments
+ var elementPairs = [[self.leftHand, self.rightHand], [self.leftForearm, self.rightForearm], [self.leftArm, self.rightArm]];
var adjustments = [{
- element: self.leftHand,
deltaX: 60,
deltaR: 0.2
}, {
- element: self.rightHand,
- deltaX: -60,
- deltaR: -0.2
- }, {
- element: self.leftForearm,
deltaX: 30,
deltaR: 0.2
}, {
- element: self.rightForearm,
- deltaX: -30,
- deltaR: -0.2
- }, {
- element: self.leftArm,
deltaX: 15,
deltaR: 0.1
- }, {
- element: self.rightArm,
- deltaX: -15,
- deltaR: -0.1
}];
- adjustments.forEach(function (adj) {
- return adjustPosition(adj.element, adj.deltaX, adj.deltaR, setGuard);
+ adjustments.forEach(function (adj, index) {
+ return adjustPosition(elementPairs[index], adj.deltaX, adj.deltaR, setGuard);
});
self.isGuarding = setGuard;
};
// Update player position based on input
clear
basic light gray convex round button with a red boxing glove icon. UI
Un gant de boxe bleu vu de dessus. video game
basic light round convex gray button with a raised blue shield icon.. UI
un éclair. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
remove
a basic white heart.. game icon
A boxer has lost the match..
man boxer with red gloves is KO on the ring..