Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (12 edits merged)
Please save this source code
User prompt
In the RoadSegment's transition function, use the stepChance var to determine stepUp and stepDown booleans calculated from the chance of stepChance succeeding
Code edit (1 edits merged)
Please save this source code
User prompt
Add a circle asset to the road class after the segment creation; with a width and height of 500, black tint, and anchor of 0.5,0.5
User prompt
Please fix the bug: 'TypeError: self is undefined' in or related to this line: 'var circle = self.attachAsset('circle', {' Line Number: 221
User prompt
Add a circle asset to the road class after the segments, with a width and height of 500, black tint, and anchor of 0.5,0.5
User prompt
remove the 2nd, duplication segment construction snippet
User prompt
Move the circle construction under the segment creation
User prompt
Add a circle asset to the road class with a width and height of 500, black tint, and anchor of 0.5,0.5
User prompt
change the background colour to sky blue
User prompt
segment.rotation should be in radians
User prompt
create two new classes: A RoadSegment class that contains the roadSegment asset (with anchor of 0,1), and a Road class containing 30 RoadSegments equally rotated around the origin. Then create and show a Road instance in the middle of the screen
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: alpha is undefined' in or related to this line: 'numberText.setText(alpha.toFixed(1));' Line Number: 67
Code edit (2 edits merged)
Please save this source code
User prompt
display a number text in the top center of the screen
Code edit (1 edits merged)
Please save this source code
Code edit (16 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: character.update is not a function' in or related to this line: 'character.update();' Line Number: 167
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ /** * config { * x : Number || 0, * y : Number || 0, * rotation : Number || 0, * } **/ var ConfigContainer = Container.expand(function (config) { var self = Container.call(this); config = config || {}; ; self.x = config.x || 0; self.y = config.y || 0; self.rotation = config.rotation || 0; if (config.scale !== undefined || config.scaleX !== undefined || config.scaleY !== undefined) { var scaleX = config.scaleX !== undefined ? config.scaleX : config.scale !== undefined ? config.scale : 1; var scaleY = config.scaleY !== undefined ? config.scaleY : config.scale !== undefined ? config.scale : 1; self.scale.set(scaleX, scaleY); } ; return self; }); var RoadSegment = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); self.attachAsset('roadSegment', { anchorX: 0, anchorY: 1 }); ; self.scaling = config.scale; self.stepChance = config.stepChance; self.transition = transition; self.previous = config.previous; ; function transition(instant) { var stepUp = Math.random() < self.stepChance; var stepDown = !stepUp && Math.random() < self.stepChance; } }); var Road = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); var segments = []; var segmentAngle = 2 * Math.PI / ROAD_SEGMENT_COUNT; for (var i = 0; i < ROAD_SEGMENT_COUNT; i++) { var segment = segments[i] = self.addChild(new RoadSegment({ previous: i > 0 ? segments[i - 1] : undefined, rotation: i * segmentAngle, stepChance: ROAD_STEP_CHANCE_BASE, scale: ROAD_SCALE_BASE })); if (i === ROAD_SEGMENT_COUNT - 1) { segments[0].previous = segment; } if (i >= ROAD_FREE_RUN_COUNT) { segment.transition(true); } } var circle = self.attachAsset('circle', { width: 500, height: 500, tint: 0x000000, anchorX: 0.5, anchorY: 0.5 }); }); var CharacterLeg = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); // Leg settings var upperBaseAngle = Math.PI * 3 / 8; var lowerBaseOffset = 135; var lowerHoldAngle = Math.PI / 8; var footBaseOffset = 100; // Create and attach leg parts var upper = self.attachAsset('upperLeg', { rotation: upperBaseAngle, anchorY: 0.1, tint: config.tint || 0xFFFFFF }); var lower = self.addChild(new ConfigContainer({ y: lowerBaseOffset, tint: config.tint || 0xFFFFFF })); lower.attachAsset('lowerLeg', { anchorX: 0.65, tint: config.tint || 0xFFFFFF }); var foot = lower.attachAsset('foot', { y: footBaseOffset, anchorX: 0.2, anchorY: 0.05, tint: config.tint || 0xFFFFFF }); self.update = function (alpha) { var sinusoidal = Math.cos(alpha * Math.PI * 2); var upperSwing = sinusoidal * Math.PI / 3; self.rotation = upperSwing; lower.rotation = lowerHoldAngle + (alpha <= 0.5 ? Math.PI / 3 + (1 - Math.abs(sinusoidal)) * Math.PI / 6 : Math.abs(-upperSwing)); foot.rotation = Math.max(0, upperSwing * 2 / 3) - lowerHoldAngle; }; }); var CharacterBody = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); // Body settings var armOffsetX = 15; var armOffsetY = 30; var pelvisOffsetX = -15; var legOffsetX = 5; // Create and attach body parts var armRight = self.addChild(new CharacterArm({ y: armOffsetY, tint: 0x777777 })); var legRight = self.addChild(new CharacterLeg({ x: pelvisOffsetX + legOffsetX, tint: 0x777777 })); var torso = self.attachAsset('torso', { anchorX: 0.5 }); var head = self.attachAsset('head', { anchorX: 0.15, anchorY: 0.95 }); var pelvis = self.attachAsset('pelvis', { x: pelvisOffsetX, y: torso.height, anchorX: 0.5, anchorY: 0.2, tint: 0xAAAAAA }); var legLeft = self.addChild(new CharacterLeg({ x: pelvisOffsetX - legOffsetX })); var armLeft = self.addChild(new CharacterArm({ y: armOffsetY })); // Position body parts armLeft.x = -torso.width / 2 + armOffsetX; armRight.x = torso.width / 2 - armOffsetX; legLeft.y = torso.height + pelvis.height / 3; legRight.y = torso.height + pelvis.height / 3; // Add an empty update function to the Character class self.runAlpha = 0; self.update = function () { this.runAlpha += 0.0075; var sinusoidal = Math.cos(this.runAlpha * Math.PI * 4); armLeft.update(this.runAlpha % 1); armRight.update((this.runAlpha - 0.5) % 1); legLeft.update(this.runAlpha % 1); legRight.update((this.runAlpha - 0.5) % 1); self.y = sinusoidal * 10; head.rotation = (1 + sinusoidal) * Math.PI / 32; }; }); var CharacterArm = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); // Arm settings var upperBaseAngle = -Math.PI * 3 / 8; var upperOffsetAngle = Math.PI / 5; var lowerBaseAngle = -Math.PI / 4; var lowerHoldAngle = -Math.PI * 5 / 8; var lowerBaseOffset = 90; // Create and attach arm parts var upper = self.attachAsset('upperArm', { rotation: upperBaseAngle, anchorX: 0.85, anchorY: 0.25, tint: config.tint || 0xFFFFFF }); var lower = self.attachAsset('lowerArm', { y: lowerBaseOffset, rotation: lowerBaseAngle + lowerHoldAngle, anchorX: 0.95, anchorY: 0.05, tint: config.tint || 0xFFFFFF }); self.update = function (alpha) { var upperSwing = Math.cos(alpha * Math.PI * 2) * Math.PI / 4; // Swing between -45 and 45 degrees var lowerSwing = -upperSwing / 2; self.rotation = -upperSwing + upperOffsetAngle; lower.rotation = lowerBaseAngle + lowerHoldAngle + lowerSwing; }; }); var Character = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); var body = self.addChild(new CharacterBody()); self.update = function () { body.update(); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue color }); /**** * Game Code ****/ // Global Settings var ROAD_SEGMENT_COUNT = 30; var ROAD_FREE_RUN_COUNT = 5; var ROAD_SCALE_BASE = 0.7; var ROAD_SCALE_STEP = 0.1; var ROAD_SCALE_MAX = 1.0; var ROAD_SCALE_MIN = 0.4; var ROAD_STEP_CHANCE_BASE = 0.1; var ROAD_STEP_CHANCE_INCREMENT = 0.5; ; // Game Instances var road = game.addChild(new Road({ x: 2048 / 2, y: 2732 / 2 })); var character = game.addChild(new Character({ x: 2048 / 2, y: 2732 / 2 })); // Create a text object to display the number var numberText = new Text2('0', { size: 150, fill: "#ffffff" }); // Set the anchor to the center of the top edge of the text numberText.anchor.set(0.5, 0); // Add the text to the top-center of the screen LK.gui.top.addChild(numberText); ; // Events LK.on('tick', function () { character.update(); }); ; // Animations function animationRun(alpha) { return {}; } function animationJump(alpha) { return {}; } function animationStand(alpha) { return {}; }
===================================================================
--- original.js
+++ change.js
@@ -14,8 +14,13 @@
;
self.x = config.x || 0;
self.y = config.y || 0;
self.rotation = config.rotation || 0;
+ if (config.scale !== undefined || config.scaleX !== undefined || config.scaleY !== undefined) {
+ var scaleX = config.scaleX !== undefined ? config.scaleX : config.scale !== undefined ? config.scale : 1;
+ var scaleY = config.scaleY !== undefined ? config.scaleY : config.scale !== undefined ? config.scale : 1;
+ self.scale.set(scaleX, scaleY);
+ }
;
return self;
});
var RoadSegment = ConfigContainer.expand(function (config) {
@@ -23,24 +28,44 @@
self.attachAsset('roadSegment', {
anchorX: 0,
anchorY: 1
});
+ ;
+ self.scaling = config.scale;
+ self.stepChance = config.stepChance;
+ self.transition = transition;
+ self.previous = config.previous;
+ ;
+ function transition(instant) {
+ var stepUp = Math.random() < self.stepChance;
+ var stepDown = !stepUp && Math.random() < self.stepChance;
+ }
});
var Road = ConfigContainer.expand(function (config) {
var self = ConfigContainer.call(this, config);
- var segmentAngle = 360 / 30;
- for (var i = 0; i < 30; i++) {
- var segment = self.addChild(new RoadSegment());
- segment.rotation = i * segmentAngle * (Math.PI / 180);
- var circle = self.attachAsset('circle', {
- width: 500,
- height: 500,
- color: 0x000000,
- anchorX: 0.5,
- anchorY: 0.5
- });
- circle.tint = 0x000000; // Set the circle's tint to black
+ var segments = [];
+ var segmentAngle = 2 * Math.PI / ROAD_SEGMENT_COUNT;
+ for (var i = 0; i < ROAD_SEGMENT_COUNT; i++) {
+ var segment = segments[i] = self.addChild(new RoadSegment({
+ previous: i > 0 ? segments[i - 1] : undefined,
+ rotation: i * segmentAngle,
+ stepChance: ROAD_STEP_CHANCE_BASE,
+ scale: ROAD_SCALE_BASE
+ }));
+ if (i === ROAD_SEGMENT_COUNT - 1) {
+ segments[0].previous = segment;
+ }
+ if (i >= ROAD_FREE_RUN_COUNT) {
+ segment.transition(true);
+ }
}
+ var circle = self.attachAsset('circle', {
+ width: 500,
+ height: 500,
+ tint: 0x000000,
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
});
var CharacterLeg = ConfigContainer.expand(function (config) {
var self = ConfigContainer.call(this, config);
// Leg settings
@@ -177,12 +202,16 @@
/****
* Game Code
****/
// Global Settings
+var ROAD_SEGMENT_COUNT = 30;
+var ROAD_FREE_RUN_COUNT = 5;
var ROAD_SCALE_BASE = 0.7;
var ROAD_SCALE_STEP = 0.1;
var ROAD_SCALE_MAX = 1.0;
var ROAD_SCALE_MIN = 0.4;
+var ROAD_STEP_CHANCE_BASE = 0.1;
+var ROAD_STEP_CHANCE_INCREMENT = 0.5;
;
// Game Instances
var road = game.addChild(new Road({
x: 2048 / 2,
white
white
circle sliced into many pieces, flat image. 2d, white background, shadowless.
pixel art of a tall, tree. game asset, 2d, white background, shadowless.
Pixel art street lamp. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pixel art cloud. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pixel art sun. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.