how I'm going to fix the if-statement on line 31. It just don't want to work. I'm using a source code that enables me to easy create 3D objects. Here's the code:
<script src="http://koda.nu/simple3d.js">
ambientLight("white");
var debug = false;
var player = {x: 0, y: 0, z: 500, item: box(0, 0, 500, 200, 200, 1, "red"), speed: 7};
var ball = {x: 0, y: 0, z: -200, item: sphere(0, 0, -200, 100, "yellow"), xSpeed: 0, ySpeed: 0, zSpeed: -15};
var enemy = {x: 0, y: 0, z: -700, item: box(0, 0, -700, 200, 200, 1, "red"), speed: 7};
box(450, 0, -100, 1, 875, 1200, "blue");
box(-450, 0, -100, 1, 875, 1200, "blue");
box(0, 450, -100, 875, 1, 1200, "blue");
box(0, -450, -100, 875, 1, 1200, "blue");
function update() {
if (keyboard.w && player.y < 350) {
player.item.translateY(player.speed);
player.y += player.speed;
}
if (keyboard.s && player.y > -350) {
player.item.translateY(-player.speed);
player.y -= player.speed;
}
if (keyboard.d && player.x < 350) {
player.item.translateX(player.speed);
player.x += player.speed;
}
if (keyboard.a && player.x > -350) {
player.item.translateX(-player.speed);
player.x -= player.speed;
}
/*(This line just don't want to work ---->)*/ if (ball.z == enemy.z) { ball.zSpeed *= -1; }
if (keyboard.shift && keyboard.y) {
debug = true;
}
ball.x += ball.xSpeed;
ball.item.translateX(ball.xSpeed);
ball.y += ball.ySpeed;
ball.item.translateY(ball.ySpeed);
ball.z += ball.zSpeed;
ball.item.translateZ(ball.zSpeed);
while (debug) {
if (keyboard.up) {
moveCameraZ(-player.speed);
}
if (keyboard.down) {
moveCameraZ(player.speed);
}
if (keyboard.left) {
moveCameraX(-player.speed);
}
if (keyboard.right) {
moveCameraX(player.speed);
}
if (keyboard.shift && keyboard.y) {
debug = True;
}
}
}
</script>
your code is very messy, i think it must be instead of { debug = True; } it must be { debug = False; }
Related
In swift I have an array like this:
var array = [ 70, 75, 0, 0, 0, 87, 90, 85, 0]
What I want to do is when the value is 0 it will copy the previous first value which is not equal to 0. So the output will become:
[ 70, 75,75, 75, 75, 87, 90, 85, 85]
I’ve tried this code below:
func fillingEmpValues(dataArray:[Int]) -> [Int?] {
var newValue = [Int]()
var array = dataArray
for (index, var element) in array.reversed().enumerated() {
if (element == 0) { // if element is 0
if(index != 0) { // if the index is not 0 proceed
array[index] = (dataArray.reversed()[index-1])
}
}
}
return newValue
}
But the codes above is not working as I expected the output is still the same. What am i MISSING? Thanks
Your code does not work because nothing is appended to newValue. Please notice the yellow warnings.
My suggestion uses a temporary variable (temp).
In the loop
if the value != 0 the value is appended and the temporary variable is set to the value.
if the value == 0 the temporary variable is appended.
func fillingEmpValues(dataArray:[Int]) -> [Int] {
var newValue = [Int]()
var temp = 0
for element in dataArray {
if element != 0 {
newValue.append(element)
temp = element
} else {
newValue.append(temp)
}
}
return newValue
}
let array = [ 70, 75, 0, 0, 0, 87, 90, 85, 0]
let filledArray = fillingEmpValues(dataArray: array) // [70, 75, 75, 75, 75, 87, 90, 85, 85]
Note: Why is your return value [Int?]? Optionals are not involved at all.
Edit:
The other condition in the comments can be accomplished with
func fillingEmpValues(dataArray:[Int]) -> [Int] {
var newValue = [Int]()
var temp = dataArray.first{ $0 != 0 } ?? 0
for (index, element) in dataArray.enumerated() {
if element != 0 {
newValue.append(element)
} else {
temp = dataArray.dropFirst(index).first{ $0 != 0 } ?? temp
newValue.append(temp)
}
}
return newValue
}
let array = [ 0, 0, 83, 0, 0, 87, 90, 85, 0]
let filledArray = fillingEmpValues(dataArray: array) // [83, 83, 83, 87, 87, 87, 90, 85, 85]
I might suggest using reduce(into:):
let array = [70, 75, 0, 0, 0, 87, 90, 85, 0]
let array2 = array.reduce(into: [Int]()) { (result, value) in
if value == 0 {
result.append(result.last ?? 0)
} else {
result.append(value)
}
}
Yielding:
[70, 75, 75, 75, 75, 87, 90, 85, 85]
I want to have a game over screen pop up, using SKSpriteNodes and it scales correctly on first try but from the second, the whole game over is small.
func spawnPlayAgainBTN() {
playAgain = SKSpriteNode(imageNamed: "buttonPlay")
playAgain.position = CGPoint(x: self.frame.midX + 54, y: self.frame.midY - 10)
playAgain.zPosition = 8
playAgain.setScale(0)
self.addChild(playAgain)
playAgain.run(SKAction.scale(to: 0.4, duration: 0.4))
}
func spawnMainMenuBTN() {
mainMenu = SKSpriteNode(imageNamed: "buttonMainMenu ")
mainMenu.position = CGPoint(x: self.frame.midX - 54, y: self.frame.midY - 10)
mainMenu.zPosition = 8
mainMenu.setScale(0)
self.addChild(mainMenu)
mainMenu.run(SKAction.scale(to: 1.0, duration: 0.4))
}
func spawnGameOverScreen() {
gameOverBG = SKSpriteNode(imageNamed: "gameOverBG")
gameOverBG.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
gameOverBG.zPosition = 7
gameOverBG.setScale(0)
self.addChild(gameOverBG)
gameOverBG.run(SKAction.scale(to: 1.0, duration: 0.4))
spawnPlayAgainBTN()
spawnMainMenuBTN()
isRunningGame = false
isShowingGameOver = true
}
I'm trying to take a series of numbers from a text file and based on the numbers define a new variable. When I print SettingsArray i get the following:
[10, 25, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0.02, 0.002, 0, 1, 0, 0, 0, 1, 1, 1, 25, 500, 0, 1, 250, 250, 250, 500, 500, 500, 10, 10, 10, 200, 200, 200]
Which are the numbers I'm looking for, but for some reason, when checking the If statements, all of the SettingsArray elements look something like this:
[2] String class name = _TtC10Foundation15_NSOpaqueString
Which obviously don't meet any of the conditions, why are the array elements garbled when checking the If statements?
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
var StepPortInvert = "00000010"
}
You just have to declare StepPortInvert once. BTW 2,3,4 = "1, 0, 0"
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
StepPortInvert = "00000010"
}
I used raphael.js to draw some element's, and now I want to connect them,
how to connect raphael elements with jsplumb ?
For example, in the JSFiddle below : I'm trying to create connections between 2 circles, but I can't, because the circles have no id, my code .
i can't use this :
jsPlumb.connect({source:"id1", target:"id2"})
I finally found a solution to connect raphael elements with jsplumb, I want to thank Mr: # Simon Porritt (jsPlumb creator) for his help: the solution
jsPlumb.ready(function(){
jsPlumb.Defaults.Container = "drawing_board";
// Create a 480 x 640 canvas.
var paper = Raphael('drawing_board');
// of 90 pixels.
var circle1 = paper.circle(140, 110, 90).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var circle2 = paper.circle(400, 180, 90).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var rect = paper.rect(50, 280, 90, 70).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var ellipse = paper.ellipse(300, 420, 90, 70).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var offsetCalculators = {
"CIRCLE":function(el, parentOffset) {
var cx = parseInt(el.attr("cx"), 10),
cy = parseInt(el.attr("cy"), 10),
r = parseInt(el.attr("r"), 10);
return {
left: parentOffset.left + (cx - r),
top:parentOffset.top + (cy - r)
};
},
"ELLIPSE":function(el, parentOffset) {
var cx = parseInt(el.attr("cx"), 10),
cy = parseInt(el.attr("cy"), 10),
rx = parseInt(el.attr("rx"), 10),
ry = parseInt(el.attr("ry"), 10);
return {
left: parentOffset.left + (cx - rx),
top:parentOffset.top + (cy - ry)
};
},
"RECT":function(el, parentOffset) {
var x = parseInt(el.attr("x"), 10),
y = parseInt(el.attr("y"), 10);
return {
left: parentOffset.left + x,
top:parentOffset.top + y
};
}
};
var sizeCalculators = {
"CIRCLE":function(el) {
var r = parseInt(el.attr("r"), 10);
return [ r * 2, r * 2 ];
},
"ELLIPSE":function(el) {
var rx = parseInt(el.attr("rx"), 10),
ry = parseInt(el.attr("ry"), 10);
return [ rx * 2, ry * 2 ];
},
"RECT":function(el) {
var w = parseInt(el.attr("width"), 10),
h = parseInt(el.attr("height"), 10);
return [ w, h ];
}
};
jsPlumb.CurrentLibrary.getOffset = function(el) {
el = $(el);
var del = el[0], tn = del.tagName.toUpperCase();
if (offsetCalculators[tn]) {
var so = el.parent().offset();
return offsetCalculators[tn](el, so);
}
else
return el.offset();
};
jsPlumb.CurrentLibrary.getSize = function(el) {
el = $(el);
var del = el[0], tn = del.tagName.toUpperCase();
if (sizeCalculators[tn]) {
return sizeCalculators[tn](el);
}
else
return [ el.outerWidth(), el.outerHeight() ];
};
jsPlumb.connect({source:circle1.node, target:circle2.node, anchor:"Center", connector:"Straight"});
jsPlumb.connect({source:circle1.node, target:rect.node, anchors:["Center", "Top"], connector:"Straight"});
jsPlumb.connect({source:circle2.node, target:ellipse.node, anchor:"Center"});
//raphael draggable
var start = function () {
this.ox = this.attr("cx");
this.oy = this.attr("cy");
},
move = function (dx, dy) {
this.attr({cx: this.ox + dx, cy: this.oy + dy});
jsPlumb.repaint(this.node);
},
up = function () { };
paper.set(circle1, circle2).drag(move, start, up); });
Is it possible to have something like this with Jqplot or Google Visualizations
So far I was able to create something similar but not entirely what I want with jqplot
Code:
var style = {
seriesDefaults: {
fill: true,
fillToZero: true,
fillAndStroke: true,
color: "rgba(190,230,110, 0.8)",
fillColor: "rgba(206,236,145, 0.8)",
shadow: false,
lineWidth: 1,
rendererOptions: {
highlightMouseOver: false
}
},
seriesColors: ["#009900", "#000099", "#00cc00", "#0000cc"],
negativeSeriesColors: ["#bb0000", "#ffe700", "#dd0000"] };
You could do something like that in the Google Visualization API, but you would have to calculate the 0-line intersections for the line and add them in as data points, then split your data into two different series (one positive and one negative). These axis crossing points will become part of your data (they will spawn tooltips when you hover over them), but it otherwise meets your requirements:
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y');
data.addColumn('boolean', 'axis-crossing point');
var y = 0;
for (var x = 0; x < 100; x++) {
y += ~~(Math.random() * 5) * Math.pow(-1, ~~(Math.random() * 2));
if (y < -50) {
y += 5;
}
if (y > 50) {
y -= 5;
}
data.addRow([x, y, false]);
}
// parse the data looking for points where the data crosses the x-axis (at y = 0)
// work backwards because we will be adding new rows to the data set
var p1, p2, m, b, intersect;
for (var i = data.getNumberOfRows() - 1; i > 0; i--) {
p1 = {x: data.getValue(i - 1, 0), y: data.getValue(i - 1, 1)};
p2 = {x: data.getValue(i, 0), y: data.getValue(i, 1)};
if ((p1.y >= 0 && p2.y < 0) || (p1.y < 0 && p2.y >= 0)) {
m = (p2.y - p1.y) / (p2.x - p1.x);
b = p1.y - m * p1.x;
intersect = -1 * b / m;
data.insertRows(i, [
[intersect, p1.y, true],
[intersect, p2.y, true]
]);
}
}
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'Positive',
calc: function (dt, row) {
var y = dt.getValue(row, 1);
return data.getValue(row, 2) ? 0 : ((y >= 0) ? y : null);
}
}, {
type: 'number',
label: 'Negative',
calc: function (dt, row) {
var y = dt.getValue(row, 1);
return data.getValue(row, 2) ? 0 : ((y < 0) ? y : null);
}
}]);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, {
height: 400,
width: 600,
vAxis: {
viewWindow: {
min: -50,
max: 50
}
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
See working example: http://jsfiddle.net/asgallant/Qc869/