Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
in the sun's update method, instead of scaling every frame, scale every 30 ticks
Code edit (1 edits merged)
Please save this source code
User prompt
add a sun asset in the sun class after the circles for-loop, with an alpha of 0.5
User prompt
In the sun class, create 10 circle assets at the origin, each one 100 units larger than the last, with the first having a width and height of 500. They should be also tinted yellow and have an alpha of 0.05
User prompt
Please fix the bug: 'TypeError: self is undefined' in or related to this line: 'self.attachAsset('circle', {' Line Number: 455
User prompt
In the sun class, create 10 circle assets at the origin, each one 100 units larger than the last, with the first having a width and height of 500. They should be also tinted yellow and have an alpha of 0.05. Make sure to keep the sun asset
User prompt
add the sun asset back
User prompt
In the sun class, create 10 circle assets at the origin, each one 100 units larger than the last, with the first having a width and height of 500. They should be also tinted yellow and have an alpha of 0.05
Code edit (3 edits merged)
Please save this source code
User prompt
Create a new Sun class to replace the sun asset
Code edit (9 edits merged)
Please save this source code
User prompt
show a sun asset at the top (slightly off screen), center of the screen
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: alpha is not defined' in or related to this line: 'if (alpha -= 0.1 < 0) {' Line Number: 164
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: fadeout is not defined' in or related to this line: 'if (fadeout && self.alpha > 0) {' Line Number: 163
Code edit (18 edits merged)
Please save this source code
User prompt
The first line of the generateBackground function should clear the background Container of any children
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
Code edit (21 edits merged)
Please save this source code
User prompt
before attaching the roadSegment asset to the RoadSegment class, attach an empty Container class
/**** * 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); var background = self.addChild(new Container()); self.attachAsset('roadSegment', { anchorX: 1, anchorY: 1 }); self.attachAsset('roadSegment', { anchorX: 1, anchorY: 1, scaleX: 0.95, scaleY: 0.95, tint: 0x555555 }); if (config.index % 3 === 0) { attachAssetRadial(self, 'square', { offset: 200, width: 100, height: 10, anchorR: 0.5, anchorX: 0.5, anchorY: 0.5 }); } ; self.update = update; self.transition = transition; self.scaling = config.scale; self.stepHeight = self.scaling * ROAD_SEGMENT_HEIGHT; self.stepChance = config.stepChance; self.previous = config.previous; ; function attachAssetRadial(parent, asset, obj) { var offsetTotal = ROAD_SEGMENT_HEIGHT - (obj.offset || 10); var rotation = -(1 - obj.anchorR) * ROAD_SEGMENT_ANGLE; parent.attachAsset(asset, { x: Math.sin(rotation) * offsetTotal, y: Math.cos(rotation) * -offsetTotal, width: obj.width, height: obj.height, anchorX: obj.anchorX, anchorY: obj.anchorY, scaleX: obj.scaleX || 1, scaleY: obj.scaleY || 1, rotation: (obj.rotation || 0) + rotation }); } function generateBackground() { background.removeChildren(); var plantCount = Math.floor(Math.random() * (ROAD_GEN_PLANT_MAX + 1)); if (Math.random() < ROAD_GEN_TREE_CHANCE) { var treeIndex = Math.floor(Math.random() * ROAD_GEN_TREE_ASSETS); var scale = 0.9 + 0.2 * Math.random(); attachAssetRadial(background, 'tree' + treeIndex, { anchorR: 0.4 + 0.2 * Math.random(), anchorX: 0.5, anchorY: 1, scaleX: scale, scaleY: scale }); } for (var i = 0; i < plantCount; i++) { var plantIndex = Math.floor(Math.random() * ROAD_GEN_PLANT_ASSETS); var scale = 0.9 + 0.2 * Math.random(); attachAssetRadial(background, 'plant' + plantIndex, { anchorR: 0.1 + 0.8 * Math.random(), anchorX: 0.5, anchorY: 1, scaleX: Math.random() < 0.5 ? -scale : scale, scaleY: scale }); } if (Math.random() < ROAD_GEN_CLOUD_CHANCE) { var cloudIndex = Math.floor(Math.random() * ROAD_GEN_CLOUD_ASSETS); var scale = 1.0 + 1.5 * Math.random(); attachAssetRadial(background, 'cloud' + cloudIndex, { offset: -800, anchorR: -1.0 + 2.0 * Math.random(), anchorX: 0.5, anchorY: 0.5, scaleX: scale, scaleY: scale }); } } function transition(instant) { var stepChance = self.previous.stepChance; var stepUp = self.previous.scaling < ROAD_SCALE_MAX ? Math.random() < self.stepChance : false; var stepDown = self.previous.scaling > ROAD_SCALE_MIN ? Math.random() < self.stepChance : false; if (stepUp || stepDown) { if (stepUp && stepDown) { if (Math.random() < 0.5) { stepDown = false; } else { stepUp = false; } } self.scaling = self.previous.scaling += ROAD_SCALE_STEP * (stepUp ? 1 : -1); self.stepChance = ROAD_STEP_CHANCE_BASE; } else { self.scaling = self.previous.scaling; self.stepChance = self.previous.stepChance + ROAD_STEP_CHANCE_INCREMENT; } generateBackground(); self.scale.set(self.scaling); self.stepHeight = self.scaling * ROAD_SEGMENT_HEIGHT; } function update() {} }); var Road = ConfigContainer.expand(function (config) { var self = ConfigContainer.call(this, config); var segments = []; for (var i = 0; i < ROAD_SEGMENT_COUNT; i++) { var segment = segments[i] = self.addChild(new RoadSegment({ index: i, previous: i > 0 ? segments[i - 1] : undefined, rotation: i * ROAD_SEGMENT_ANGLE, 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); } } self.attachAsset('circle', { width: 500, height: 500, tint: 0x222222, anchorX: 0.5, anchorY: 0.5 }); self.attachAsset('circle', { width: 485, height: 485, tint: 0x87CEEB, anchorX: 0.5, anchorY: 0.5 }); var destroyIndex = -1; ; self.update = update; self.getIndex = getIndex; self.getNode = getNode; ; function update() { self.rotation -= PLAYER_ROTATIONAL_SPEED; var newDestroyIndex = getIndex(ROAD_SEGMENT_DESTROY); if (newDestroyIndex > 0 && newDestroyIndex !== destroyIndex) { destroyIndex = newDestroyIndex; segments[destroyIndex].transition(); } } function getIndex(shift) { return Math.floor(-self.rotation / MATH_2_PI * ROAD_SEGMENT_COUNT + (shift || 0) + 1) % ROAD_SEGMENT_COUNT; } function getNode(shift) { return segments[getIndex(shift)]; } }); 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.animate = animate; ; function animate(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 baseY = config.y || 0; 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.animate = animate; ; function animate() { this.runAlpha += 0.02; var sinusoidal = Math.cos(this.runAlpha * Math.PI * 4); armLeft.animate(this.runAlpha % 1); armRight.animate((this.runAlpha - 0.5) % 1); legLeft.animate(this.runAlpha % 1); legRight.animate((this.runAlpha - 0.5) % 1); self.y = baseY + 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.animate = animate; ; function animate(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({ y: -400 })); var baseY = config.y; ; self.update = update; ; function update() { body.animate(); self.y = baseY - road.getNode().stepHeight; } }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue color }); /**** * Game Code ****/ // Game Constants // Global Constants & Settings var GAME_TICKS = 60; var GAME_WIDTH = 2048; var GAME_HEIGHT = 2732; // Math Constants var MATH_2_PI = Math.PI * 2; var MATH_QUARTER_PI = Math.PI / 4; // Road Constants var ROAD_SEGMENT_COUNT = 30; var ROAD_SEGMENT_ANGLE = MATH_2_PI / ROAD_SEGMENT_COUNT; var ROAD_SEGMENT_HEIGHT = 1000; var ROAD_SEGMENT_DESTROY = -4; 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; var ROAD_GEN_PLANT_ASSETS = 3; var ROAD_GEN_PLANT_MAX = 3; var ROAD_GEN_TREE_ASSETS = 4; var ROAD_GEN_TREE_CHANCE = 0.1; var ROAD_GEN_CLOUD_ASSETS = 4; var ROAD_GEN_CLOUD_CHANCE = 0.2; // Player Constants var PLAYER_ROTATIONAL_SPEED = MATH_2_PI / (GAME_TICKS * 10); // 10s to complete a revolution ; // Game Instances var road = game.addChild(new Road({ x: GAME_WIDTH / 2, y: GAME_HEIGHT - GAME_WIDTH / 2 })); var character = game.addChild(new Character({ x: road.x, y: road.y, scale: 0.5 })); ; // Animations function animationRun(alpha) { return {}; } function animationJump(alpha) { return {}; } function animationStand(alpha) { return {}; }
===================================================================
--- original.js
+++ change.js
@@ -24,11 +24,9 @@
return self;
});
var RoadSegment = ConfigContainer.expand(function (config) {
var self = ConfigContainer.call(this, config);
- var backgroundContainer = self.addChild(new Container());
- var backgroundOld = undefined;
- var backgroundNew = backgroundContainer.addChild(new Container());
+ var background = self.addChild(new Container());
self.attachAsset('roadSegment', {
anchorX: 1,
anchorY: 1
});
@@ -40,9 +38,9 @@
tint: 0x555555
});
if (config.index % 3 === 0) {
attachAssetRadial(self, 'square', {
- offset: 100,
+ offset: 200,
width: 100,
height: 10,
anchorR: 0.5,
anchorX: 0.5,
@@ -72,13 +70,14 @@
rotation: (obj.rotation || 0) + rotation
});
}
function generateBackground() {
+ background.removeChildren();
var plantCount = Math.floor(Math.random() * (ROAD_GEN_PLANT_MAX + 1));
if (Math.random() < ROAD_GEN_TREE_CHANCE) {
var treeIndex = Math.floor(Math.random() * ROAD_GEN_TREE_ASSETS);
var scale = 0.9 + 0.2 * Math.random();
- attachAssetRadial(backgroundNew, 'tree' + treeIndex, {
+ attachAssetRadial(background, 'tree' + treeIndex, {
anchorR: 0.4 + 0.2 * Math.random(),
anchorX: 0.5,
anchorY: 1,
scaleX: scale,
@@ -87,16 +86,28 @@
}
for (var i = 0; i < plantCount; i++) {
var plantIndex = Math.floor(Math.random() * ROAD_GEN_PLANT_ASSETS);
var scale = 0.9 + 0.2 * Math.random();
- attachAssetRadial(backgroundNew, 'plant' + plantIndex, {
+ attachAssetRadial(background, 'plant' + plantIndex, {
anchorR: 0.1 + 0.8 * Math.random(),
anchorX: 0.5,
anchorY: 1,
scaleX: Math.random() < 0.5 ? -scale : scale,
scaleY: scale
});
}
+ if (Math.random() < ROAD_GEN_CLOUD_CHANCE) {
+ var cloudIndex = Math.floor(Math.random() * ROAD_GEN_CLOUD_ASSETS);
+ var scale = 1.0 + 1.5 * Math.random();
+ attachAssetRadial(background, 'cloud' + cloudIndex, {
+ offset: -800,
+ anchorR: -1.0 + 2.0 * Math.random(),
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: scale,
+ scaleY: scale
+ });
+ }
}
function transition(instant) {
var stepChance = self.previous.stepChance;
var stepUp = self.previous.scaling < ROAD_SCALE_MAX ? Math.random() < self.stepChance : false;
@@ -152,18 +163,27 @@
tint: 0x87CEEB,
anchorX: 0.5,
anchorY: 0.5
});
+ var destroyIndex = -1;
;
self.update = update;
+ self.getIndex = getIndex;
self.getNode = getNode;
;
function update() {
self.rotation -= PLAYER_ROTATIONAL_SPEED;
+ var newDestroyIndex = getIndex(ROAD_SEGMENT_DESTROY);
+ if (newDestroyIndex > 0 && newDestroyIndex !== destroyIndex) {
+ destroyIndex = newDestroyIndex;
+ segments[destroyIndex].transition();
+ }
}
+ function getIndex(shift) {
+ return Math.floor(-self.rotation / MATH_2_PI * ROAD_SEGMENT_COUNT + (shift || 0) + 1) % ROAD_SEGMENT_COUNT;
+ }
function getNode(shift) {
- var index = Math.floor(-self.rotation / MATH_2_PI * ROAD_SEGMENT_COUNT) % ROAD_SEGMENT_COUNT;
- return segments[index];
+ return segments[getIndex(shift)];
}
});
var CharacterLeg = ConfigContainer.expand(function (config) {
var self = ConfigContainer.call(this, config);
@@ -323,12 +343,14 @@
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
// Math Constants
var MATH_2_PI = Math.PI * 2;
+var MATH_QUARTER_PI = Math.PI / 4;
// Road Constants
var ROAD_SEGMENT_COUNT = 30;
var ROAD_SEGMENT_ANGLE = MATH_2_PI / ROAD_SEGMENT_COUNT;
var ROAD_SEGMENT_HEIGHT = 1000;
+var ROAD_SEGMENT_DESTROY = -4;
var ROAD_FREE_RUN_COUNT = 5;
var ROAD_SCALE_BASE = 0.7;
var ROAD_SCALE_STEP = 0.1;
var ROAD_SCALE_MAX = 1.0;
@@ -336,10 +358,12 @@
var ROAD_STEP_CHANCE_BASE = 0.1;
var ROAD_STEP_CHANCE_INCREMENT = 0.5;
var ROAD_GEN_PLANT_ASSETS = 3;
var ROAD_GEN_PLANT_MAX = 3;
-var ROAD_GEN_TREE_ASSETS = 3;
+var ROAD_GEN_TREE_ASSETS = 4;
var ROAD_GEN_TREE_CHANCE = 0.1;
+var ROAD_GEN_CLOUD_ASSETS = 4;
+var ROAD_GEN_CLOUD_CHANCE = 0.2;
// Player Constants
var PLAYER_ROTATIONAL_SPEED = MATH_2_PI / (GAME_TICKS * 10); // 10s to complete a revolution
;
// Game Instances
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.