はじめまして。と質問

78 views
Skip to first unread message

平田佳寛

unread,
Sep 23, 2013, 10:20:52 PM9/23/13
to coro...@googlegroups.com
はじめまして。プログラマー志望2年目の者です。
Lua経験は1週間ないです。
現在、海外で4人でプロジェクトを進めています。
早速質問です。
UIのボタンをクラス化しようと思い、作ってみたのですが、正常に動作せず困っています。
画像Aが表示されていてタッチイベントが発生すると画像Bが画像Aと同じ座標に表示されるようにしたしました。しかしボタンを複数表示させると画像Bの位置が最後にnewしたボタンの位置に表示されてしまいます。



ButtonTest ={};
ButtonTest.new = function()
this = {};
this.pos = {};
this.pos.x = display.contentWidth/2;
this.pos.y = display.contentHeight/2;
this.offset = {};
this.offset.x = nil;
this.offset.y = nil;
this.Image = {};
this.Image.a = {};
this.Image.a.File = "画像のアドレス.拡張子";
this.Image.a.Show = nil;
print(this.Image.a.File);
this.Image.b = {};
this.Image.b.File = "画像のアドレス.拡張子";
this.Image.b.Show = nil;
this.Set = function()
this.Image.a.Show = display.newImage(this.Image.a.File);
this.Image.a.Show.x = this.pos.x;
this.Image.a.Show.y = this.pos.y;
end;
this.Run = function( event )
if "began" == event.phase then
this.Image.b.Show = display.newImage(this.Image.b.File);
this.Image.b.Show.x = this.pos.x;
this.Image.b.Show.y = this.pos.y;
print("testpos");
print(this.Image.b.Show.x);
this.Shape = Triangle;
--print(this.Image.b.Show.y);
--this.Image.b.Show.x = this.pos.x;
--this.Image.b.Show.y = this.pos.y;
end;
if "moved" == event.phase then
if (event.x <= event.target.x + event.target.width/2-5 and event.x >= event.target.x - event.target.width/2+5 
and event.y <= event.target.y + event.target.height/2-5  and event.y >= event.target.y - event.target.height/2+5 )then
-- In Box
else
if(this.Image.b.Show ~= nil) then
this.Image.b.Show:removeSelf();
this.Image.b.Show = nil;
end;
end;
end;
if "ended" == event.phase or "cancelled" == event.phase then
if(this.Image.b.Show ~= nil) then
this.Image.b.Show:removeSelf();
this.Image.b.Show = nil;
end;
--this.Image.a.Show:removeEventListener("touch",this.Run);
end;
print("Success!!")
--this.Image.a:addEventListener("touch",this.Run);
return true;
end;
this.Test = function()
this.Image.a.Show = display.newImage(this.Image.a.File);
this.Image.a.Show.x = this.pos.x;
this.Image.a.Show.y = this.pos.y;
this.Image.b.Show = display.newImage(this.Image.b.File);
this.Image.b.Show.x = this.pos.x/2;
this.Image.b.Show.y = this.pos.y/2;
end;
return this;
end;


使い方例:
local test = ButtonTest.new();
test.Set();
test.Image.a.Show:addEventListener("touch",test.Run);




助言よろしくお願いします。

hiratay

unread,
Sep 26, 2013, 11:55:45 PM9/26/13
to coro...@googlegroups.com
自己解決しました。
ソースコードを載せておきます。

-------------------------------------
-- ButtonClass.lua
-------------------------------------
module("ButtonClass", package.seeall)


new = function(ImageA,ImageB)
local this = {};
this.a = display.newImage( ImageA );
this.b = nil;
this.a.x = display.contentWidth/2;
this.a.y = display.contentHeight/2;
this.beganEvent = function()end;
this.movedEvent = function()end;
this.endedEvent = function()end;
this.Run = function(event)
if event.phase == "began" then
if(this.b ~= nil)then
this.b:removeSelf();
this.b = nil;
end;
this.b = display.newImage( ImageB );
this.b.x = this.a.x;
this.b.y = this.a.y;
this.beganEvent();
elseif event.phase == "moved" then
if (event.x <= event.target.x + event.target.width/2-5 and event.x >= event.target.x - event.target.width/2+5 
and event.y <= event.target.y + event.target.height/2-5  and event.y >= event.target.y - event.target.height/2+5 )then
--in box
this.movedEvent();
else
if (this.b ~= nil) then
this.b:removeSelf();
this.b = nil;
end;
end;
end;
if event.phase == "ended" or event.phase == "cancelled" then
if (this.b ~= nil) then
this.b:removeSelf();
this.b = nil;
this.endedEvent();
end;
end;
return ture;
end;
return this;
end

さっき調べていたらwidget.newButton()というものがあるじゃないか・・・

ありがとうございました。

Reply all
Reply to author
Forward
0 new messages