suppose i have simple composable fun which has Canvas.
Canvas(modifier = Modifier
.padding(start = 60.dp, end = 60.dp)
.fillMaxSize(),
onDraw = {
val w = size.width
val h = size.height
val s1LineOffset = w / 4 - 10
val s2LineOffset = w * 3 / 8 - 10
drawImage(
image = ImageBitmap.imageResource(
res = resources,
id = R.drawable.bttn
),
topLeft = Offset(
x = b1StartOffset,
y = 0f
)
)
}
)
I want to define the state of animation using the canvas sizes for initial and target value but i cant do so because i can't use this inside draw scope thats why i have to use it above the Canvas block, hence cant access Canvas size what should i do
val anim by ballAnim.animateFloat(
initialValue = ,
targetValue =,
animationSpec =
)
It is more of a simple job. Just create and remember a MutableState<T> value, then update it inside Canvas. For example,
var canvasSize by remember { mutableStateOf(IntSize()) }
Canvas(modifier = Modifier
.padding(start = 60.dp, end = 60.dp)
.fillMaxSize(),
onDraw = {
canvasSize = size //Assign it here
val w = size.width
val h = size.height
val s1LineOffset = w / 4 - 10
val s2LineOffset = w * 3 / 8 - 10
drawImage(
image = ImageBitmap.imageResource(
res = resources,
id = R.drawable.bttn
),
topLeft = Offset(
x = b1StartOffset,
y = 0f
)
)
}
)
val anim by ballAnim.animateFloat(
initialValue = canvasSize.width,
targetValue = canvasSize.height, //whatever
animationSpec = spring()
)
One of the options is animating some normalized value and denormalizing it using size inside onDraw, e.g.:
val animNormalized by ballAnim.animateFloat(
initialValue = 0.5,
targetValue = 0.8,
animationSpec =
)
Canvas(modifier = Modifier
.padding(start = 60.dp, end = 60.dp)
.fillMaxSize(),
onDraw = {
val w = size.width
val h = size.height
val anim = animNormalized * w
...
}
)
I'm trying to draw some sprites where the alpha channel of the image is taken into account.
What is the correct set of values for the following structures to support alpha channel of textures in the fragment shader?
vk::PipelineColorBlendAttachmentState colorBlendAttachment;
colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eOne;
colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eZero;
colorBlendAttachment.colorBlendOp = vk::BlendOp::eAdd;
colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eOne;
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eZero;
colorBlendAttachment.alphaBlendOp = vk::BlendOp::eSubtract;
vk::PipelineColorBlendStateCreateInfo colorBlending;
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.logicOp = vk::LogicOp::eCopy;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
colorBlending.blendConstants[0] = 0.0f;
colorBlending.blendConstants[1] = 0.0f;
colorBlending.blendConstants[2] = 0.0f;
colorBlending.blendConstants[3] = 0.0f;
Per Ekzusy's answer, here are 2 ways:
Using the 'discard' keyword in the fragment shader.
// Read data from some texture.
vec4 color = texture(...);
// This makes the alpha channel (w component) act as a boolean.
if (color.w < 1) { discard; }
For my original question, these values will do:
vk::PipelineColorBlendAttachmentState colorBlendAttachment;
colorBlendAttachment.colorWriteMask =
vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
colorBlendAttachment.colorBlendOp = vk::BlendOp::eAdd;
colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha;
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
colorBlendAttachment.alphaBlendOp = vk::BlendOp::eSubtract;
I'm writing a scenario to a game which allows to use custom scripts.
I want to change the camera angle using SetCameraHeading(float in_fAngle), where:
"0 is facing east, 90 is facing north, 180 is facing west, and 270 is facing south".
I made something like this:
//The map size is 150x300:
NWX = 0;
NWY = 300;
NEX = 150;
NEY = 300;
SWX = 0;
SWY = 0;
SEX = 150;
SEY = 0;
Player1NW = GetDistanceFromUnitToPoint("Survivor1", NWX, NWY);
Player1NE = GetDistanceFromUnitToPoint("Survivor1", NEX, NEY);
Player1SW = GetDistanceFromUnitToPoint("Survivor1", SWX, SWY);
Player1SE = GetDistanceFromUnitToPoint("Survivor1", SEX, SEY);
Player1NWDiff = Player1NWOld - Player1NW;
Player1NEDiff = Player1NEOld - Player1NE;
Player1SWDiff = Player1SWOld - Player1SW;
Player1SEDiff = Player1SEOld - Player1SE;
//Need to set camera heading here:
SetCameraHeading(Player1CurrentAngle, false);
Player1NWOld = Player1NW;
Player1NEOld = Player1NE;
Player1SWOld = Player1SW;
Player1SEOld = Player1SE;
The problem is I don't know how to change the angle depending on position changes (for example, when an unit is walking it changes to: Player1NWDiff = -0.02, Player1NEDiff = -0.02, Player1SWDiff = 0.04, Player1SEDiff = 0.03 etc.)
Does anybody know how should I calculate it to get a correct angle (0 to 360)?
Here's a problem: I'm making a simple physics game using Corona SDK and very simple module: http://developer.coronalabs.com/code/move-camera So I've got fish object with physics body and in each frame (enterFrame event) I'm centering camera on it
camera.x = -fish.x + screenWidth / 2
And I've got two backgrounds bg1 and bg2, which tiled each after other (bg1 bg2 bg1 bg2 etc) will make nice moving-world effect without user knowing that there are only 2 backgrounds. The problem is I can't get right calculations of their positions to make such effect. I googled a lot, but can't find anything to fit my needs.
Here's a free art, which I'm using http://www.vickiwenderlich.com/2013/02/free-game-art-flying-goldfish/ There you can find these two backgrounds.
It's also possible that fish can go slightly in opposite direction (generally it's moving to the right so to make world-moving effect backgrounds has to move to the left) so it would be nice if it'll in calculations.
I figured out something like this (however it doesn't work but maybe I'm close):
local function updateFrame(event)
local m
local dfx = lfx - fish.x
lfx = fish.x
camera.x = -fish.x + screenWidth / 2
--print("dfx = " .. dfx)
bg1.x = bg1.x + dfx
bg2.x = bg2.x + dfx
s = s + dfx
if mAbs(s) > screenWidth then
m = sgn(s)
if m ~= 0 then
s = s - m * screenWidth
if m < 0 then
bg1.x = bg1.x - 2 * m * screenWidth
else
bg2.x = bg2.x - 2 * m * screenWidth
end
end
end
end
Runtime:addEventListener( "enterFrame", updateFrame );
s = screenLeft - screenWidth
where s is the sum of pixels moved and if it's over screenWidth I jump with one of the backgrounds, lfx is the last fish x location, dfx is a difference between last and current fish location and that's it.
If you can help me with my calculations or give me new ones or just give some interesting links please write them right here.
Any help will be appreciated,
Regards
local road = display.newImageRect( "Images/roadBg1.png",2247/4,559/4 )
road:setReferencePoint( display.CenterLeftReferencePoint )
road.x = 0
road.y = baseline - 20
local road2 = display.newImageRect( "Images/roadBg1.png",2247/4,559/4 )
road2:setReferencePoint( display.CenterLeftReferencePoint )
road2.x = 2247/4
road2.y = baseline - 20
local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = ( 0.2 * tDelta )
road.x = road.x - xOffset
road2.x = road2.x - xOffset
if (road.x + road.contentWidth) < 0 then
road:translate( 2247/4 * 2, 0)
end
if (road2.x + road2.contentWidth) < 0 then
road2:translate( 2247/4 * 2, 0)
end
Runtime:addEventListener( "enterFrame", move )
or see the sample code of jungle scene game.
Location: SampleApps/Sprites/JungleScene
Try this:
local bg1 = display.newImageRect( "bg_1.png" ,480 ,320)
bg1.x = display.contentWidth/2; bg1.y = display.contentHeight/2
local bg2 = display.newImageRect( "bg_2.png" ,480 ,320 )
bg2.x = bg1.x + bg1.width; bg2.y = display.contentHeight/2
local speed = 30
function move()
bg1.x = bg1.x-speed;
bg2.x = bg2.x-speed;
if(bg1.x + bg1.width/2 < 0)then bg1.x = bg1.width*3/2-speed
elseif(bg2.x + bg2.width/2 < 0)then bg2.x = bg2.width*3/2-speed end
end
Runtime:addEventListener( "enterFrame", move )
Keep coding............ :)
Thanks #krs and #Malar, combining your answers I figured out how to do this:
local function updateFrame(event)
local speed = fish.x - lfx
lfx = fish.x
camera.x = -fish.x + screenWidth / 2
bg1.x = bg1.x - speed
bg2.x = bg2.x - speed
if bg1.x + bg1.width < 0 then
bg1.x = bg1.x + screenWidth * 2
end
if bg2.x + bg2.width < 0 then
bg2.x = bg2.x + screenWidth * 2
end
if bg1.x - bg1.width > 0 then
bg1.x = bg1.x - screenWidth * 2
end
if bg2.x - bg2.width > 0 then
bg2.x = bg2.x - screenWidth * 2
end
end
Runtime:addEventListener( "enterFrame", updateFrame );
Starting positions:
bg1.x = screenLeft
bg2.x = bg1.x + bg1.width
And the last most important change:
I put bg1, bg2 and camera into a group and other objects into camera group:
local camera = require("camera")
group = display.newGroup()
group:insert(bg1)
group:insert(bg2)
camera:insert(fish)
camera:insert(ground)
group:insert(camera)
So we have ground and fish, which can move forever, but the backgrounds are only moving from -screenWidth to screenWidth.
All these things combined works like a charm!
I have created a menu with 2 buttons , and i am using storyboard API to move around scene , but it only works for the first button , for the other button it gives a black screen , I am positive there's nothing wrong in SCENE 2 , because i tried to set both scenes ( SCENE 1 and SCENE 2 ) the same code , and it worked with SCENE 1 but still gives a black image for SCENE 2 , here's my menu code , hopefully you can fix the error :
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("start.png")
background:setReferencePoint(display.BottomLeftReferencePoint)
background.x = 0
background.y = 320
background.speed = 1
screenGroup:insert(background)
city2 = display.newImage("city2.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 0
city2.y = 320
screenGroup:insert(city2)
play = display.newImage("play.png")
play.x = 242
play.y = 161
screenGroup:insert(play)
controls = display.newImage("controls.png")
controls.x = 144
controls.y = 201
screenGroup:insert(controls)
function SCENE1(event)
if event.phase == "began" then
storyboard.gotoScene("SCENE1", "fade", 400)
end
end
function SCENE2(event)
if event.phase == "began" then
storyboard.gotoScene("SCENE2", "fade", 400)
end
end
function scene:enterScene(event)
play:addEventListener("touch", SCENE1)
controls:addEventListener("touch", SCENE2)
end
function scene:exitScene(event)
play:removeEventListener("touch", SCENE1)
controls:removeEventListener("touch", SCENE2)
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
FOR SCENE 2 :
module(..., package.seeall)
-- requires
local physics = require "physics"
physics.start()
require "sprite"
score = require ("score")
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
-- background
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("bg.png")
screenGroup:insert(background)
scoreInfo = score.getInfo()
score.init({
x = 40,
y = 5}
)
score.setScore(0)
ceiling = display.newImage("invisibleTile.png")
ceiling:setReferencePoint(display.BottomLeftReferencePoint)
ceiling.x = 0
ceiling.y = 0
physics.addBody(ceiling, "static", {density=.1, bounce=0.1, friction=.2})
screenGroup:insert(ceiling)
theFloor = display.newImage("invisibleTile.png")
theFloor:setReferencePoint(display.BottomLeftReferencePoint)
theFloor.x = 0
theFloor.y = 340
physics.addBody(theFloor, "static", {density=.1, bounce=0.1, friction=.2})
screenGroup:insert(theFloor)
city1 = display.newImage("city1.png")
city1:setReferencePoint(display.BottomLeftReferencePoint)
city1.x = 0
city1.y = 320
city1.speed = 1
screenGroup:insert(city1)
city2 = display.newImage("city1.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 480
city2.y = 320
city2.speed = 1
screenGroup:insert(city2)
city3 = display.newImage("city2.png")
city3:setReferencePoint(display.BottomLeftReferencePoint)
city3.x = 0
city3.y = 320
city3.speed = 2
screenGroup:insert(city3)
city4 = display.newImage("city2.png")
city4:setReferencePoint(display.BottomLeftReferencePoint)
city4.x = 480
city4.y = 320
city4.speed = 2
screenGroup:insert(city4)
jetSpriteSheet = sprite.newSpriteSheet("jet.png", 50, 17)
jetSprites = sprite.newSpriteSet(jetSpriteSheet, 1, 4)
sprite.add(jetSprites, "jets", 1, 4, 1000, 0)
jet = sprite.newSprite(jetSprites)
jet.x = -80
jet.y = 100
jet:prepare("jets")
jet:play()
jet.collided = false
physics.addBody(jet, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(jet)
jetIntro = transition.to(jet,{time=2000, x=100, onComplete=jetReady})
explosionSpriteSheet = sprite.newSpriteSheet("explosion.png", 24, 23)
explosionSprites = sprite.newSpriteSet(explosionSpriteSheet, 1, 8)
sprite.add(explosionSprites, "explosions", 1, 8, 2000, 1)
explosion = sprite.newSprite(explosionSprites)
explosion.x = 100
explosion.y = 100
explosion:prepare("explosions")
-- explosion:play()
explosion.isVisible = false
-- physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(explosion)
mine1 = display.newImage("mine.png")
mine1.x = math.random(480,500)
mine1.y = math.random(1,100)
mine1.speed = math.random(2,6)
mine1.initY = mine1.y
mine1.amp = math.random(20,90)
mine1.angle = math.random(1,360)
physics.addBody(mine1, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine1)
mine2 = display.newImage("mine.png")
mine2.x = math.random(900,950)
mine2.y = math.random(1,100)
mine2.speed = math.random(2,6)
mine2.initY = mine2.y
mine2.amp = math.random(20,100)
mine2.angle = math.random(1,360)
physics.addBody(mine2, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine2)
mine3 = display.newImage("mine.png")
mine3.x = math.random(1450,1570)
mine3.y = math.random(300,340)
mine3.speed = math.random(2,6)
mine3.initY = mine3.y
mine3.amp = math.random(20,70)
mine3.angle = math.random(1,360)
physics.addBody(mine3, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine3)
mine4 = display.newImage("mine.png")
mine4.x = math.random(2500,2770)
mine4.y = math.random(330,330)
mine4.speed = math.random(2,6)
mine4.initY = mine3.y
mine4.amp = math.random(20,100)
mine4.angle = math.random(1,360)
physics.addBody(mine4, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine4)
mine6 = display.newImage("mine.png")
mine6.x = math.random(4000,4770)
mine6.y = math.random(1,320)
mine6.speed = math.random(2,6)
mine6.initY = mine6.y
mine6.amp = math.random(20,80)
mine6.angle = math.random(1,360)
physics.addBody(mine6, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine6)
mine5 = display.newImage("mine.png")
mine5.x = math.random(5500,5770)
mine5.y = math.random(1,130)
mine5.speed = math.random(2,6)
mine5.initY = mine5.y
mine5.amp = math.random(20,80)
mine5.angle = math.random(1,360)
physics.addBody(mine5, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine5)
end
function scrollCity(self,event)
if self.x < -477 then
self.x = 480
else
self.x = self.x - self.speed
end
end
function moveMines(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(90,220)
self.speed = math.random(2,6)
self.amp = math.random(20,100)
self.angle = math.random(1,360)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp*math.sin(self.angle)+self.initY
end
end
function addtoit (event)
if event.phase == "ended" then
score.setScore (score.getScore()+1)
end
end
function jetReady()
jet.bodyType = "dynamic"
end
function activateJets(self,event)
self:applyForce(0, -1.5, self.x, self.y)
print("run")
end
function touchScreen(event)
print("touch")
if event.phase == "began" then
jet.enterFrame = activateJets
Runtime:addEventListener("enterFrame", jet)
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", jet)
end
end
function gameOver()
storyboard.gotoScene("restart", "fade", 400)
end
function explode()
explosion.x = jet.x
explosion.y = jet.y
explosion.isVisible = true
explosion:play()
jet.isVisible = false
timer.performWithDelay(3000, gameOver, 1)
end
local taSound = audio.loadSound("GAME.mp3")
local tapSound = audio.loadSound("explosion-01.wav")
function onCollision(event)
if event.phase == "began" then
if jet.collided == false then
jet.collided = true
jet.bodyType = "static"
explode()
audio.stop()
audio.play(tapSound ,{ duration=3100 } )
background:removeEventListener("touch", addtoit)
end
end
end
function scene:enterScene(event)
storyboard.purgeScene("start")
storyboard.purgeScene("restart")
Runtime:addEventListener("touch", touchScreen)
city1.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city1)
city2.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city2)
city3.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city3)
city4.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city4)
mine1.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine1)
mine2.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine2)
mine3.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine3)
mine4.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine4)
mine5.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine5)
mine6.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine6)
Runtime:addEventListener("collision", onCollision)
audio.play(taSound)
background:addEventListener("touch", addtoit)
end
function scene:exitScene(event)
Runtime:removeEventListener("touch", touchScreen)
Runtime:removeEventListener("enterFrame", city1)
Runtime:removeEventListener("enterFrame", city2)
Runtime:removeEventListener("enterFrame", city3)
Runtime:removeEventListener("enterFrame", city4)
Runtime:removeEventListener("enterFrame", mine1)
Runtime:removeEventListener("enterFrame", mine2)
Runtime:removeEventListener("enterFrame", mine3)
Runtime:removeEventListener("enterFrame", mine4)
Runtime:removeEventListener("collision", onCollision)
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
i think the problem is in the scene:enterScene you try to load your city object over and over again at the same time there are so many runtime listener in your code that it repeats calling the same object.
are you creating a sidescrolling game? if so you can try to see this link on how to create side scrolling game from scratch http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-side-scroller-from-scratch/
hopes this helps