HI, I have a js code that draws and moves a snake, I want to transfer it to defold, but I have some difficulties…
/main/scripts/player.script:27: bad argument #2 to 'set_position' (url expected, got userdata)
stack traceback:
[C]:-1 : in function set_position
/main/scripts/player.script:27: in function </main/scripts/player.script:23>
code on JS:
let x = [],
y = [],
segNum = 20,
segLength = 18;
for (let i = 0; i < segNum; i++) {
x[i] = 0;
y[i] = 0;
}
function setup() {
createCanvas(710, 400);
strokeWeight(9);
stroke(255, 100);
}
function draw() {
background(0);
dragSegment(0, mouseX, mouseY);
for (let i = 0; i < x.length - 1; i++) {
dragSegment(i + 1, x[i], y[i]);
}
}
function dragSegment(i, xin, yin) {
const dx = xin - x[i];
const dy = yin - y[i];
const angle = atan2(dy, dx);
x[i] = xin - cos(angle) * segLength;
y[i] = yin - sin(angle) * segLength;
segment(x[i], y[i], angle);
}
function segment(x, y, a) {
push();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
pop();
}
code on Defold:
local mousePos = vmath.vector3(0,0,0)
local x = {}
local y = {}
local segNum = 20
local seg = "#segm"
local obj = {}
function init(self)
msg.post(".", "acquire_input_focus")
for i = 0, segNum, 1 do
x[i] = 0
y[i] = 0
local onj = factory.create("/go#factory", vmath.vector3(x[i], y[i], 0), nil, {}, 1.0)
table.insert(obj, msg.url(onj))
print(obj[i]);
end
end
function update(self, dt)
dragSegment(0,mousePos.x,mousePos.y)
for i = 0, #x-1, 1 do
dragSegment(i+1,x[i],y[i])
go.set_position("/instance0",vmath.vector3(x[i],y[i],0))
end
end
function dragSegment(i,xin,yin)
local dx = xin-x[i]
local dy = yin-y[i]
local angle = math.atan2(dy,dx)
x[i] = xin - math.cos(angle) * 1
y[i] = yin - math.sin(angle) * 1
segment(x[i],y[i],a)
end
function segment(_x, _y, a)
end
function on_input(self, action_id, action)
mousePos = vmath.vector3(action.x, action.y, 0)
end
how it was supposed to work
Thanks to everyone who read this. I will be very grateful to everyone who will help.