So i have this problem where my SKAction dosen't run properly. Sometimes it runs by duration of 0.2 but most of the times it is instant no animation of the node moving to its position.
array_dot[j].runAction(SKAction.moveToY(premakni_do, duration: 0.2))
More code where this is used. This is the function to move the Sphere Nodes to one place down if the place is free underneath them (idk if this code is good but it works kinda :P). Also 152 on Y is the max as low as they can go.
What i do is run through all the nodes that match the names then move them to an array, reverse that array and then move them as low as they can go. But as mentioned before the moving of the Nodes works, but most of the times (90%) the animation doesn't run. Also i am not using any of the physics.
func premakni_pike()
{
array_dot.removeAll()
var preveri_pozicija: CGFloat = 0
self.enumerateChildNodesWithName("//*")
{
node, stop in
if(node.name == "blue" || node.name == "yellow" || node.name == "red" || node.name == "green")
{
self.array_dot.append(node)
}
}
let velikost_array = array_dot.count
array_dot = array_dot.reverse()
var premakni_do: CGFloat = 0
for j in 0...velikost_array-1
{
if(array_dot[j].position.y != 156)
{
premakni_do = array_dot[j].position.y
for i in 0...6
{
preveri_pozicija = array_dot[j].position.y - 60
if(preveri_pozicija < 156)
{
break
}
print(array_dot[j].name,i,preveri_pozicija,array_dot[j].position.y)
let novi_node = self.nodeAtPoint(CGPoint(x: array_dot[j].position.x, y: preveri_pozicija))
if(array_dot[j].intersectsNode(novi_node))
{
array_dot[j].position.y = preveri_pozicija
premakni_do = premakni_do - 60
print("PRAZNO")
}
else
{
break
}
}
array_dot[j].runAction(SKAction.moveToY(premakni_do, duration: 0.2))
}
}
}
Are you sure runAction is not working? It may be that you are setting the nodes position and then telling it to move to that position. Try removing the lines
if(array_dot[j].intersectsNode(novi_node))
{
array_dot[j].position.y = preveri_pozicija
premakni_do = premakni_do - 60
print("PRAZNO")
}
else
{
break
}
Related
Most important code just for example. Func UPDATE is called in every draw frame.
I want implement jumping virtualJumpActive == true logic.
I invert procedure for virtualJumpActive == true to make scene object and camera position set by cannonjs position but just in some short time.
My camera.posY follow fixed values for now.
I wanna only Y (for cannonjs Z / jumping ) to be navigated by physics and other XY
to be navigated by camera (WASD user control)...
It is little intricate situation, i have 3 factors
position of camera
position of player (hands)
position of player collision object
In normal regime player collision object follow camera and player.
In jump moment i wanna apply by vertical force and make camera follow collision object. Also i wanna that always Y component depens on physics data. In that way i will have jumping on top of other objects.
var playerUpdater = {
UPDATE: () => {
App.scene[objName].rotation.rotateY(
matrixEngine.Events.camera.yaw + 180)
var detPitch;
var limit = 2;
if(matrixEngine.Events.camera.pitch < limit &&
matrixEngine.Events.camera.pitch > -limit) {
detPitch = matrixEngine.Events.camera.pitch * 2;
} else if(matrixEngine.Events.camera.pitch > limit) {
detPitch = limit * 2;
} else if(matrixEngine.Events.camera.pitch < -(limit + 2)) {
detPitch = -(limit + 2) * 2;
}
if(matrixEngine.Events.camera.virtualJumpActive == true) {
// invert logic
// Scene object set
App.scene[objName].rotation.rotateX(-detPitch);
var detPitchPos = matrixEngine.Events.camera.pitch;
if(detPitchPos > 4) detPitchPos = 4;
App.scene[objName].position.setPosition(
App.scene.playerCollisonBox.physics.currentBody.position.x,
App.scene.playerCollisonBox.physics.currentBody.position.z,
App.scene.playerCollisonBox.physics.currentBody.position.y
)
// Cannonjs object set
// Switched Z - Y
matrixEngine.Events.camera.xPos = App.scene.playerCollisonBox.physics.currentBody.position.x;
matrixEngine.Events.camera.zPos = App.scene.playerCollisonBox.physics.currentBody.position.y;
matrixEngine.Events.camera.yPos = App.scene.playerCollisonBox.physics.currentBody.position.z;
// App.scene.playerCollisonBox.
// physics.currentBody.velocity.set(0, 0, 0);
App.scene.playerCollisonBox.
physics.currentBody.angularVelocity.set(0, 0, 0);
setTimeout(() => {
matrixEngine.Events.camera.virtualJumpActive = false;
matrixEngine.Events.camera.virtualJumpY = 2;
}, 350);
} else {
// Scene object set
App.scene[objName].rotation.rotateX(-detPitch);
var detPitchPos = matrixEngine.Events.camera.pitch;
if(detPitchPos > 4) detPitchPos = 4;
App.scene[objName].position.setPosition(
matrixEngine.Events.camera.xPos,
matrixEngine.Events.camera.yPos - 0.3 + detPitchPos / 50,
matrixEngine.Events.camera.zPos,
)
// Cannonjs object set
// Switched Z - Y
App.scene.playerCollisonBox.
physics.currentBody.position.set(
matrixEngine.Events.camera.xPos,
matrixEngine.Events.camera.zPos,
matrixEngine.Events.camera.yPos);
App.scene.playerCollisonBox.
physics.currentBody.velocity.set(0, 0, 0);
App.scene.playerCollisonBox.
physics.currentBody.angularVelocity.set(0, 0, 0);
}
}
};
.full {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
<object class="full" data="https://fps-matrix-engine.vercel.app"/>
Any suggestion ?
More data:
Stackoverflow Question ref code =>
https://codepen.io/zlatnaspirala/pen/eYKrmdM?editors=0010
Source code github
Last update
https://fps-matrix-engine.vercel.app
I'm using plotbands in a gauge chart to represent angle ranges. But I'm facing a problem with the plotband angles when the "from" value is higher than the "to" value.
JSFiddle
As you can see, the plotband is set as from: 270, to: 45 but it really is rendered as if it was set as from: 45, to: 270. That renders exactly the oposite angle range that I need.
The only way that I can find to do that is setting two plotbands, one from 270 to 360 and another one from 0 to 45, but that seems very unconvenient.
Is there any easy way to achieve what I'm trying to do?
As I have mentioned in my comment, I think that you should be able to override getPlotBand method in your code for enabling plotBands with bigger from value than to value:
(function(H) {
H.wrap(H.Axis.prototype, 'init', function(proceed, chart, userOptions) {
this.getPlotBandPath = function(from, to, options) {
var center = this.center,
startAngleRad = this.startAngleRad,
pick = H.pick,
map = H.map,
pInt = H.pInt,
fullRadius = center[2] / 2,
radii = [
pick(options.outerRadius, '100%'),
options.innerRadius,
pick(options.thickness, 10)
],
offset = Math.min(this.offset, 0),
percentRegex = /%$/,
start,
end,
open,
isCircular = this.isCircular, // X axis in a polar chart
ret;
// Polygonal plot bands
if (this.options.gridLineInterpolation === 'polygon') {
ret = this.getPlotLinePath(from).concat(this.getPlotLinePath(to, true));
// Circular grid bands
} else {
// Keep within bounds
from = Math.max(from, this.min);
to = Math.min(to, this.max);
// Plot bands on Y axis (radial axis) - inner and outer radius depend on to and from
if (!isCircular) {
radii[0] = this.translate(from);
radii[1] = this.translate(to);
}
// Convert percentages to pixel values
radii = map(radii, function(radius) {
if (percentRegex.test(radius)) {
radius = (pInt(radius, 10) * fullRadius) / 100;
}
return radius;
});
// Handle full circle
if (options.shape === 'circle' || !isCircular) {
start = -Math.PI / 2;
end = Math.PI * 1.5;
open = true;
} else {
start = startAngleRad + this.translate(from);
end = startAngleRad + this.translate(to);
}
radii[0] -= offset; // #5283
radii[2] -= offset; // #5283
ret = this.chart.renderer.symbols.arc(
this.left + center[0],
this.top + center[1],
radii[0],
radii[0], {
start: start, // Math is for reversed yAxis (#3606)
end: end,
innerR: pick(radii[1], radii[0] - radii[2]),
open: open
}
);
}
return ret;
}
proceed.call(this, chart, userOptions);
});
}(Highcharts))
Live example:
http://jsfiddle.net/2Ljk7usL/9/
I have written a function which currently displays all of the prime numbers under 1000.
I can keep making 1000 bigger to generate more numbers, but I do not know how to make it keep going on forever once run.
func generatePrimes() {
let numbers = 2...1000
for n in numbers {
var prime = true
for i in 2..<n {
if n % i == 0 {
prime = false
break
}
}
if prime == true {
print(n)
}
}
}
You could use a while loop with the argument being true. That way, the loop would never end.
func generatePrimes() {
var n = 2
while true {
var prime = true
for i in 2..<n {
if n % i == 0 {
prime = false
break
}
}
if prime == true {
print(n)
}
n += 1
}
}
Note that we use a variable (n) and increment (add 1 to it) on each iteration of the while loop.
If you want it to end when it hits some n, you can have a conditional that checks against n and break's out of the loop:
var n = 2
while true {
if n == 1000 {
break
}
n += 1
}
The above code will stop execution when n hits 1000 and will not print anything for that case.
Use a while loop instead of for loop. It can also be done with a for loop, but for loop is preferred when you know the number or range of the iterations you are performing. If you're using a while loop you just have to set the condition for the loop, in this case you need to specify the condition as true since you want the loop to run forever.
Just replace your for loop in your code with while. It should look something like this
while(true)
{
if n % i == 0 {
prime = false
break
}
}
You also can build an unconditional infinite loop using do {} block and continue statement.
AGAIN:
do {
print("step...")
continue AGAIN
}
func generatePrimes() {
var prime = true
let numbers = 2...1000
for n in numbers {
prime = true
if n == 2 || n == 3 {
print(n)
} else {
for i in 2..<n-1 {
if n % i == 0 {
//not prime numbers
prime = false
}
}
if prime == true {
print(n)
}
}
}
}
You can do it like this. Just exclude 1 and n.
I am trying to remove a node from a linked list if its value falls within a certain range (greater than or equal to low and less than or equal to high). This code only removes the first value found within the range.
public void removeData(E low, E high) {
Node previousNode = root;
Node deleteNode = previousNode.getNext();
while (deleteNode != null) {
if (deleteNode.getValue().compareTo(low) >= 0 && deleteNode.getValue().compareTo(high) <= 0) {
previousNode.setNext(deleteNode.getNext());
}
previousNode = previousNode.getNext();
deleteNode = deleteNode.getNext();
}
}
In this code what you need to do is:
public void removeData(E low, E high) {
Node previousNode = root;
Node deleteNode = previousNode.getNext();
while (deleteNode != null) {
if (deleteNode.getValue().compareTo(low) >= 0 && deleteNode.getValue().compareTo(high) <= 0) {
previousNode.setNext(deleteNode.getNext());
}else{
previousNode = previousNode.getNext();
}
deleteNode = deleteNode.getNext();
}
}
because if one node is deleted the previous node doesn't need to incremented just deleteNode has to. and check for another deleteNode.
Hope this may help you.
Thanks!!
following image is showing the negative life of the player.... because intersecting with obstacles in the way...i want the solution for only decreasing life by one point after intersecting.
if (Texture.playerrect.Intersects(Texture.mirchirect) || Texture.playerrect.Intersects(Texture.crabrect) || Texture.playerrect.Intersects(Texture.stonerect) || Texture.playerrect.Intersects(Texture.cactusrect))
{
die = true;
currentframe = 19;
}
else { die = false; }
if (die)
{
life -= 1;
}
Maybe it's because the code is updating 60 frames per second and die == true, then the life of your object would decrease by 60 each second, instead of 1.
possible way... add one bollean variable PlayerHitted to your obstacles, and when collision happened set life -= 1 if PlayerHitted is false (by default) and update PlayerHitted to true.
that way obstacles will have information if they hit player
if (Texture.playerrect.Intersects(Texture.mirchirect) || Texture.playerrect.Intersects(Texture.crabrect) || Texture.playerrect.Intersects(Texture.stonerect) || Texture.playerrect.Intersects(Texture.cactusrect))
{
die = true;
currentframe = 19;
if (!Update)
{
life--;
Update = true;
}
}
else { die = false; Update = false; }