忍者ブログ

姉好き変態プログラマーの備忘録

色んな言語に手を出そうとする自分へのメモ・・・

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

CoronaSDK Simulator サンプルコード 解説&メモ FlashLight

何も書かないというのもあれなので、久々にサンプルコードを載せようかなと。。。
ということで、今回もサンプルページ内のflashlightというサンプルをもとに色々やっていきたいと思います。

拍手


このアプリをざっと説明すると、、、
照らせます
以上。


そりゃまぁフラッシュライトだものねぇ
照らせなきゃ嘘だよねぇ

まぁいいや
えー、まずはスクショから

こんな感じになります。


んで、もとの画面?というか下地?はこんな感じ

ようはこの画面を光で照らすというアプリですね
サンプルコードは以下

display.setStatusBar( display.HiddenStatusBar )

local halfW = display.contentCenterX
local halfH = display.contentCenterY

-- Black background
local bkg = display.newRect( 0, 0, 768, 1024 )
bkg:setFillColor( 0 )

-- Image to be masked
local image = display.newImageRect( "image.png", 768, 1024 )
image:translate( halfW, halfH )

-- Mask
local mask = graphics.newMask( "circlemask.png" )
image:setMask( mask )


local radiusMax = math.sqrt( halfW*halfW + halfH*halfH )

function onTouch( event )
	local t = event.target

	local phase = event.phase
	if "began" == phase then
		display.getCurrentStage():setFocus( t )

		-- Spurious events can be sent to the target, e.g. the user presses 
		-- elsewhere on the screen and then moves the finger over the target.
		-- To prevent this, we add this flag. Only when it's true will "move"
		-- events be sent to the target.
		t.isFocus = true

		-- Store initial position
		t.x0 = event.x - t.maskX
		t.y0 = event.y - t.maskY
	elseif t.isFocus then
		if "moved" == phase then
			-- Make object move (we subtract t.x0,t.y0 so that moves are
			-- relative to initial grab point, rather than object "snapping").
			local maskX = event.x - t.x0
			local maskY = event.y - t.y0
			t.maskX = maskX
			t.maskY = maskY

			-- Stretch the flashlight as it moves further away 
			-- from the screen's center
			local radius = math.sqrt( maskX*maskX + maskY*maskY )
			local scaleDelta = radius/radiusMax
			t.maskScaleX = 1 + scaleDelta
			t.maskScaleY = 1 + 0.2 * scaleDelta

			-- Rotate it appropriately about screen center
			local rotation = math.deg( math.atan2( maskY, maskX ) )
			t.maskRotation = rotation

		elseif "ended" == phase or "cancelled" == phase then
			display.getCurrentStage():setFocus( nil )
			t.isFocus = false
		end
	end

	return true
end
image:addEventListener( "touch", onTouch )

-- By default, the mask will limit the touch region to areas that lie inside 
-- both the mask and the image being masked.  We can override this by setting the
-- isHitTestMasked property to false, so the touch region lies inside the entire image. 
image.isHitTestMasked = false

-- Display instructions
local labelFont = "Zapfino" -- if not found, system font will be used
local myLabel = display.newText( "Touch circle to move flashlight", 0, 0, labelFont, 34 )
myLabel:setTextColor( 255, 255, 255, 180 )
myLabel.x = halfW
myLabel.y = 200

では解説に入っていこうかと
色々細かい説明は省いてある程度重要な所を中心に説明します
12行目
image:translate( halfW, halfH )
ですが、最初は座標を指定するものかと思いましたが、リファレンスで調べると、
object:translate( deltaX, deltaY )
とあったので、現在の状態からどれだけ変化させるかということだと思います。
こうすると画像の参照点(画像の位置を指定する際に、移動の基準となる点。左上、中心などの点)を気にせずに移動できる。
まぁ
object.x = object.x + deltaX
とかやれば同じことできますね。

16行目
image:setMask(msk)
このマスクってのがそもそも意味が読み取れない。。。
黄緑色したハゲ男?
と思っても致し方ないですよね
PR

Comment

buy generic arimidex anastrozole

uqb how much does anastrozole cost <a href=http://svydidjfne.com>arimidex anastrozole price</a>, why do men take anastrozole
お名前
タイトル
E-MAIL
URL
コメント
パスワード

Copyright © 姉好き変態プログラマーの備忘録 : All rights reserved

TemplateDesign by KARMA7

忍者ブログ [PR]

管理人限定

プロフィール

HN:
うみ
性別:
男性
趣味:
ソフト開発、ゲーム、機械弄り
自己紹介:
色んな言語に手を出そうとしています
よくわからないことを自分のためにメモします
たまに別のこともつぶやきます
姉しよっ!の海お姉ちゃんが大好きです
・・・変態です

最新コメント

[04/25 Smithe385]
[04/12 名無しの弟]
[04/08 名無しの弟]
[03/29 名無しの弟]
[03/21 名無しの弟]

カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

ブログ内検索