Actually I'm working from a tutorial and there is some error in that tutorial.
There is the code:
class "Game"( Graphics )
Game.menuScreen = nil
Game.gameScreen = nil
Game.achScreen = nil
Game.screen = nil
-- main
function Game:main()
-- Create the screens and store their links within the class
Game.menuScreen = MainMenu.new()
Game.gameScreen = GameScreen.new()
Game.achScreen = AchScreen.new()
-- Display menuScreen
Game.showScreen( 'menuScreen' )
end
-- showScreen
function Game.showScreen( name )
-- If a screen is being displayed - remove it from the stage
if Game.screen then
Stage.detach( Game.screen )
Game.screen = nil
end
-- Retrieve a screen link by name
local screen = Game[name]
if not screen then
return nil
end
-- If the screen is found - add it to the stage
Stage.attach( screen )
-- Save the displayed screen
Game.screen = screen
return screen
end
It write to me [string "Game.script"]:11: attempt to index global 'MainMenu' (a nil value)
I use Dreemchest Composer and this one is the tutirul: http://dreemchest.com/doc/en/Game%20menu%20and%20screen.html
Actually I remowed the LEVEL select screen from this code, cos I don't want to implement a level selection to my first game in this.
I have a script named MainMenu and it's class's MainMenu, Superclass is soMainMenu.
I have got it, thank you!
I had to create Stage Objects for all menu points to get it work. It's easy but the tutorial may didn't wrote it or I just didn't understand it.
Related
I am relatively new to Swift.
I am building an application in which I would like to swipe through an array of AR assets; essentially swipe right or left and a new .scn model is rendered and interacts with the scene. However, loadedContentForAssets only takes a value string and inputting a var initiates a search for a model of the same name (that naturally doesn't exist). Any help is greatly appreciated or pointers in the general direction.
Cheers.
P.S.
Here is what I am using to implement the transition so far (faceOverlayArray is an array of SCNNodes). I would like to allow navigating/swiping to the end and then loop back to the index 0. Seeing two errors:
Cannot convert value of type '(Any) -> Int' to expected argument type '(UnboundedRange_) -> ()
and
Cannot assign to property: 'index' is a method.
`func didSwipeRight() {
let arrayLength = faceOverlayArray.count
var faceOverlayContent = self.faceOverlayArray[index]
index = (index < faceOverlayArray.count-1) ? index+1 : 0
}`
if (c.class == "Google-chrome") then
c.icon = capi.image ( "/home/art-attack/.config/awesome/icons/chrome.png" )
end
I tried it but I always got an error i.e attempt to call field 'image' (a nil value)
{ rule = { class = "Google-chrome" },
properties = { icon = beautiful.icon_chrome } },
Then I find another way to use properties icon in awful.rules and it worked but didn't change the icon instead it disabled the icon of that client.
To fix your first attempt, try this:
if c.class == "Google-chrome" then
local icon = gears.surface("path/to/chrome.png")
c.icon = icon._native
icon:finish()
end
The line with icon:finish() is not necessary, but it exists to make sure you do not optimize this code. There is some dark garbage collection avoidance magic in there that you do not want to know, but the short story is: Never use _native unless you already have the surface itself saved in a variable.
if c.class == "Google-chrome" then
local icon = gears.surface("path/to/chrome.png")
return text, bg, bg_image, icon
end
Add this code before
if capi.client.focus == c then
local background = display.newImage("black.png", 0, 0)
local submit = display.newImage("submit.png")
submit.x = display.contentWidth/2
submit.y = display.contentHeight-100
local nameInstructions = display.newText("Enter your name", 10, 50, native.systemFont, 24)
local usersName = native.newTextField(10, 100, 350, 50)
usersName.inputType = "default"
local function keyboardListener (event)
native.setKeyboardFocus(nil)
end
background:addEventListener("tap", keyboardListener)
local function reverseName(event)
reverseUsersName = string.reverse(usersName)
end
submit:addEventListener("tap", reverseName)
local reverse = display.newText(reverseUsersName)
reverse.x = display.contentWidth/2
reverse.y = display.contentHeight/2
Every time that I run this using my Corona SDK thing, I get this:
Bad argument #-1 to 'newText' (string expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'newText'
...Corona Projects/Assignment 4.3/main.lua/src/main.lua:24: in main chunk
reverseName is in the local function reverseName(event), but this function is called once u press or tap on the submit . But here local reverse = display.newText(reverseUsersName), is called before you tap on the sumbit . That is why its giving you error.
The only place that reverseUsersName, accessed on line 24, is set, is inside the function reverseName(event). In this function, reverseUsersName is a global so after the function is run once, that variable will be accessible from other parts of your script, but until then, it does not exist.
Now in line 22 you've registered reverseName as event listener for "tap" events, but events are only generated after your script has been executed once (and in between calls to your script callbacks like reverseName and keyboardListener are callbacks), so when you create the display text just after, the variable does not yet exist.
So what you would have to do is update the text of the reverse display item in your reverseName listener so that every time you click on the button, the reversed name becomes visible. And because of that, you would have to declare your reverse variable above reverseName function so that it is available as an upvalue (read the Corona getting started docs, they are excellent and discuss this subtlety) in that function. And presumably you would want to initialize with showing the usersName rather than reverse.
So you would need something like
local reverse = display.newText(usersName)
local function reverseName(event)
reverseUsersName = string.reverse(usersName)
reverse.SetText(reverseUserName)
end
submit:addEventListener("tap", reverseName)
Note that if you want the string being displayed to be reversed every time you press tap, rather than only first time you press it, you will have to use
local function reverseName(event)
reverseUsersName = string.reverse(reverseUsersName)
reverse.SetText(reverseUserName)
end
reverseUsersName = usersName
Check your function reverseName and the text object(reverse) with the following code:
local reverse --[[ Initialize the object with a global scope,
so you can access it anywhere from the page. --]]
local function reverseName(event)
--[[ In the below line, usersName is a table value. It is the reason
of the error. For getting the string from the text field, you
have to provide 'usersName.text' --]]
reverseUsersName = string.reverse(usersName.text)
reverse.text = reverseUsersName -- Assign the text field value to your text object
end
submit:addEventListener("tap", reverseName)
reverse = display.newText("",20,20,nil,20) --see the parameters of display.newText()*
reverse.x = display.contentWidth/2
reverse.y = display.contentHeight/2
*Corona API: display.newText()
I have a lua table like this:
local defaultSize = 14
local field = {
sizeA = defaultSize,
sizeB = sizeA,
}
My intention is to set sizeB to the value of field.sizeA, however the code above does not work. field.sizeB is nil in this case.
How to set sizeB to whatever sizeA is inside the table definition directly?
You could have an init function in table and call that:
local defaultSize = 14
local field = {
init = function (self, size)
self.sizeA = size or defaultSize -- size only if given, otherwise defaultSize
self.sizeB = self.sizeA
end
}
field:init() -- implicit "self" arg is "field", defaultSize will be used
field:init(16) -- size will be 16 rather than 14
print(field.sizeB) -- prints 14
This has the clear advantage of aggregating all initialization of table instance in one place, you can have conditionals etc once your logic gets more complex. You don't have to have init() as member of table, but it is always a good idea to keep dependencies obvious and close together.
I'm learning F#, and decided to try making simple XNA games for windows using F# (pure enthusiasm) , and got a window with some images showing up.
Here's the code:
(*Methods*)
member self.DrawSprites() =
_spriteBatch.Begin()
for i = 0 to _list.Length-1 do
let spentity = _list.List.ElementAt(i)
_spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)
_spriteBatch.End()
(*Overriding*)
override self.Initialize() =
ChangeGraphicsProfile()
_graphicsDevice <- _graphics.GraphicsDevice
_list.AddSprite(0,"NagatoYuki",992.0,990.0)
base.Initialize()
override self.LoadContent() =
_spriteBatch <- new SpriteBatch(_graphicsDevice)
base.LoadContent()
override self.Draw(gameTime : GameTime) =
base.Draw(gameTime)
_graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
self.DrawSprites()
And the AddSprite Method:
member self.AddSprite(ID : int,imageTexture : string , width : float, height : float) =
let texture = content.Load<Texture2D>(imageTexture)
list <- list # [new SpriteEntity(ID,list.Length, texture,Vector2.Zero,width,height)]
The _list object has a ContentManager, here's the constructor:
type SpriteList(_content : ContentManager byref) =
let mutable content = _content
let mutable list = []
But I can't minimize the window, since when it regains its focus, i get this error:
ObjectDisposedException
Cannot access a disposed object.
Object name: 'GraphicsDevice'.
What is happening?
Well after struggling for some time I got it to work. But it doesn't seem "right"
(thinking that way, using XNA and F# doesn't seem right either, but it's fun.)
(*Methods*)
member self.DrawSprites() =
_spriteBatch.Begin()
for i = 0 to _list.Length-1 do
let spentity = _list.List.ElementAt(i)
if spentity.ImageTexture.IsDisposed then
spentity.ImageTexture <- _list.Content.Load<Texture2D>(spentity.Name)
_spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)
_spriteBatch.End()
(*Overriding*)
override self.Initialize() =
ChangeGraphicsProfile()
_list.AddSprite(0,"NagatoYuki",992.0,990.0)
base.Initialize()
override self.LoadContent() =
ChangeGraphicsProfile()
_graphicsDevice <- _graphics.GraphicsDevice
_spriteBatch <- new SpriteBatch(_graphicsDevice)
base.LoadContent()
I adjust the graphicsDevice whenever my game needs to LoadContent, and in the DrawSprites() method I check if the texture is disposed, if it is, load it up again.
But this thing bugs me. I didn't know I had to Load all Content again everytime the window is minimized.
(And the code makes it look like Initialize() loads Content, and LoadContent() initializes, but oh well)
What you are observing is normal behaviour, it's by the way not specific to F#. See http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspx
This method is called by Initialize. Also, it is called any time the game content needs to be reloaded, such as when the DeviceReset event occurs.
Are you loading all of your content in Game.LoadContent? If you do, you should not be getting these errors.