如何在JavaScript中创建坐标对象动态?

时间:2013-08-26 01:54:51

标签: javascript titanium titanium-mobile titanium-modules

我正在使用Titanium Appcelerator的模块来绘制线条。在行中你只需要2个带坐标的对象来绘制一条线(起点和终点)。

我想用'touchmove'事件修改起点/终点对象,但是我对JavaScript很新,我无法用正确的方法来解决它。

var lines = require('com.lines'); //<< Import lines module

var attrib = { 
    color:'red',
    touchEnable:true,
    thickness:20
};

var from = {x:20,y:100}; //<< Start Point Object Coordinates
var to = {x:200,y:100}; //<< End POint Object Coordinates
var lineOne = lines.createLine(from, to, attrib); //<<Creates lineOne var with attributes
win.add(lineOne); //Draw the line. 

使用lineOne变量移动整行没有问题,但正如我所说,我找不到动态修改对象的方法。

谢谢!

更新 - 在Josiah的建议之后,我能够成功修改“to”变量,但无论如何,该行都会重新绘制原始的“to”值。我正在使用setTo方法不正确,或者我需要其他东西来更新和重绘行..

var attrib = {
    color:'red',
    touchEnable:true,
    thickness:20
};

var from = {x:20,y:100}; //Insert touchMoveFunction <<-- Here! 
var to = {x:200,y:100};
var lineOne = lines.createLine(from, to, attrib);

function drawline(e){
    win.add(lineOne);
};

alert(to);

var lineOne = lines.createLine(from,to,attrib);

var buttonPosition = Ti.UI.createButton({
    title:'CLick.Me!',
    bottom:0
});
win.add(buttonPosition);

buttonPosition.addEventListener('click',function(e){
    to = {x:200,y:300};
    lineOne.setTo(to);
    drawline();
    alert(to);
});

1 个答案:

答案 0 :(得分:1)

只需将touchmove的事件监听器附加到窗口(或任何持有行的视图容器),然后刷新,或者更改行所需的任何内容,这是一个小例子:

var lineOne = lines.createLine(from, to, attrib);
win.add(lineOne);
win.addEventListener('touchmove', function(e) {
    win.remove(lineOne);
    var touchX = e.x;
    var touchY = e.y;
    var to = {x:touchX,y:touchY}; 
    lineOne = lines.createLine(from, to, attrib);
    // Add a new line
    win.add(lineOne);
});