Difference between revisions of "Pixel"
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
Line 37: | Line 37: | ||
stage:addChild(p) | stage:addChild(p) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Moving stars in a gradient sky''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | -- a gradient bg | ||
+ | local bggradient = Pixel.new(0xffffff, 1, 800, 480) -- set a white color to start with | ||
+ | bggradient:setColor(0x0, 1, 0x290e2c, 1, -90) -- a gradient set horizontally | ||
+ | bggradient:setAnchorPoint(0.5, 0.5) | ||
+ | -- stars | ||
+ | local spritestars = Sprite.new() -- a sprite to hold all star shapes | ||
+ | for i = 1, 1024 do -- @PaulH | ||
+ | local s = Shape.new() | ||
+ | s:setLineStyle(2, 0xffffff) | ||
+ | s:moveTo(0,0) | ||
+ | s:lineTo(0,0) | ||
+ | s:endPath() | ||
+ | s:setPosition(math.random(0, 800*2), math.random(0, 480*2)) | ||
+ | s:setScale(math.random(1,2)) | ||
+ | s:setAlpha(math.random(10,50)/100) | ||
+ | spritestars:addChild(s) | ||
+ | end | ||
+ | -- render stars to a render target (rt is in portrait mode) | ||
+ | local rtstars = RenderTarget.new(spritestars:getHeight(), spritestars:getWidth(), nil, true, nil, nil, nil, false) | ||
+ | rtstars:draw(spritestars) | ||
+ | -- create an image (Pixel) of the stars | ||
+ | local bgstars = Pixel.new(rtstars, spritestars:getWidth(), spritestars:getHeight()) | ||
+ | bgstars:setAnchorPoint(0.5, 0.5) | ||
+ | local texscale = 0.8 | ||
+ | bgstars:setTextureScale(texscale, texscale) | ||
+ | -- position | ||
+ | bggradient:setPosition(800/2, 480/2) | ||
+ | bgstars:setPosition(spritestars:getWidth()/2, spritestars:getHeight()/2) | ||
+ | -- order | ||
+ | stage:addChild(bggradient) | ||
+ | stage:addChild(bgstars) | ||
+ | |||
+ | -- game loop | ||
+ | local velx = 0 | ||
+ | function onEnterFrame(e) | ||
+ | -- move the stars | ||
+ | velx -= e.deltaTime * 256 | ||
+ | bgstars:setTexturePosition(velx, 0) | ||
+ | end | ||
+ | stage:addEventListener(Event.ENTER_FRAME, onEnterFrame) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
+ | |||
=== Methods === | === Methods === | ||
[[Pixel.new]] ''creates a new Pixel''<br/><!--GIDEROSMTD:Pixel.new(color,alpha,width,height) creates a new Pixel--> | [[Pixel.new]] ''creates a new Pixel''<br/><!--GIDEROSMTD:Pixel.new(color,alpha,width,height) creates a new Pixel--> |
Revision as of 00:36, 18 July 2023
Supported platforms:
Available since: Gideros 2016.06
Inherits from: Sprite
Description
A rectangular Sprite which can be filled with solid colors, gradients or textures. Pixel aims at being a simpler and faster alternative to Shape when needing to display a coloured box or box with a gradient. It is also useful as Bitmap replacement since every texture will be fitted into Pixel dimensions automatically.
Examples
local mypixel = Pixel.new(0x0000FF, 0.75, 128, 128)
mypixel:setAnchorPoint(0.5, 0.5)
mypixel:setPosition(application:getContentWidth() / 2, 64)
stage:addChild(mypixel)
application:setBackgroundColor(0x323232)
local p = Pixel.new(0xffffff, 1, 32, 32)
p:set("redMultiplier", 1) -- OK
p:set("greenMultiplier", 0) -- OK
p:set("blueMultiplier", 0) -- OK
p:set("alphaMultiplier", 1) -- OK
--p:set("anchorX", .5) -- NOT OK
--p:set("anchorY", .5) -- NOT OK
p:set("anchorX", 16) -- OK
p:set("anchorY", 16) -- OK
p:set("alpha", .5) -- OK
p:set("scaleX", 1.5) -- OK
p:set("rotation", 10) -- OK
p:set("x", 32) -- OK
p:set("y", 32) -- OK
stage:addChild(p)
Moving stars in a gradient sky
-- a gradient bg
local bggradient = Pixel.new(0xffffff, 1, 800, 480) -- set a white color to start with
bggradient:setColor(0x0, 1, 0x290e2c, 1, -90) -- a gradient set horizontally
bggradient:setAnchorPoint(0.5, 0.5)
-- stars
local spritestars = Sprite.new() -- a sprite to hold all star shapes
for i = 1, 1024 do -- @PaulH
local s = Shape.new()
s:setLineStyle(2, 0xffffff)
s:moveTo(0,0)
s:lineTo(0,0)
s:endPath()
s:setPosition(math.random(0, 800*2), math.random(0, 480*2))
s:setScale(math.random(1,2))
s:setAlpha(math.random(10,50)/100)
spritestars:addChild(s)
end
-- render stars to a render target (rt is in portrait mode)
local rtstars = RenderTarget.new(spritestars:getHeight(), spritestars:getWidth(), nil, true, nil, nil, nil, false)
rtstars:draw(spritestars)
-- create an image (Pixel) of the stars
local bgstars = Pixel.new(rtstars, spritestars:getWidth(), spritestars:getHeight())
bgstars:setAnchorPoint(0.5, 0.5)
local texscale = 0.8
bgstars:setTextureScale(texscale, texscale)
-- position
bggradient:setPosition(800/2, 480/2)
bgstars:setPosition(spritestars:getWidth()/2, spritestars:getHeight()/2)
-- order
stage:addChild(bggradient)
stage:addChild(bgstars)
-- game loop
local velx = 0
function onEnterFrame(e)
-- move the stars
velx -= e.deltaTime * 256
bgstars:setTexturePosition(velx, 0)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
MethodsPixel.new creates a new Pixel |
EventsConstants |