• What do you need help with? Version 1
    5,001 replies, posted
All the tutorials I've seen are for console apps, and don't work when I try them for a forms application like I am.
[QUOTE=Chad Mobile;21126777]All the tutorials I've seen are for console apps, and don't work when I try them for a forms application like I am.[/QUOTE] Are you creating the Lua object in the main function? If so, make it a member of the class of the form you'd be using it in and create it in the constructor (or maybe the button OnClick event, not sure [probably not]), register your functions after you create it, and then call DoString on the textbox text when the button is pressed.
Ugh, that's so confusing.
What doesn't work about them? One potential difference that comes to mind is that when the Lua code calls the "print" command, the output probably goes to the application's standard output stream by default, which you can't see in a GUI app. Wishfallen mentioned a RegisterFunction function that you can call on a Lua object to make custom commands available in the Lua interpreter. You could probably use that to replace the standard print command with your own version, that takes the text and displays it in your window. [editline]05:36PM[/editline] [QUOTE=Chad Mobile;21126965]Ugh, that's so confusing.[/QUOTE] Yes, that's not very straightforwardly written. What Wishfallen is saying is: [list] [*]You have a class for your application's main form. [*]You should make the Lua object be a member variable in that class. [*]You should initialize that variable (i.e. actually create the Lua object) in the form class's constructor. [*]If you're registering any custom functions with RegisterFunction, you should do that in the form class's constructor too. [*]In the method that handles a click on the button, take the contents of the textbox and call DoString() on the Lua object with it. [/list]
[QUOTE=Wyzard;21127114]What doesn't work about them? One potential difference that comes to mind is that when the Lua code calls the "print" command, the output probably goes to the application's standard output stream by default, which you can't see in a GUI app. Wishfallen mentioned a RegisterFunction function that you can call on a Lua object to make custom commands available in the Lua interpreter. You could probably use that to replace the standard print command with your own version, that takes the text and displays it in your window.[/QUOTE] io.stdout and io.stdin (to prevent a "freeze") should also be replaced (or just plain removed) if that's your tactic. It might actually be easier to hook into the standard in and out pipes of your program.
[QUOTE=Wyzard;21127114] Yes, that's not very straightforwardly written. What Wishfallen is saying is: [list] [*]You have a class for your application's main form. [*]You should make the Lua object be a member variable in that class. [*]You should initialize that variable (i.e. actually create the Lua object) in the form class's constructor. [*]If you're registering any custom functions with RegisterFunction, you should do that in the form class's constructor too. [*]In the method that handles a click on the button, take the contents of the textbox and call DoString() on the Lua object with it. [/list][/QUOTE] Yeah, that's exactly what I meant. Apologies for the confusion, am a bit sleepy just now.
I'll try to figure it out soon, I guess I should read up on it more.
ITT Spoonfeeding
Game.Services don't seem to be as straightforward as I thought. Can someone explain that and GameComponent class to me and how it applies to my problem (in one or to pages before)? Like, if I do Services.AddService(typeof(spriteBatch), spriteBatch); in Game1 class, how do I get spriteBatch from my DisplayObject class?
Need help in java: I've taken a semester's worth of java programming and I'm beginning to create a strategy game. I've looked around google for a class that creates a [b]2D grid and will allow pictures to be used and updated in each element[/b]. I know that JFrame from the java library can be used to construct a 2D grid, but most of the GUI classes from the standard java API are a mess to figure out. To my knowledge, loading images onto a JFrame grid isn't even possible. If anyone knows a written class or set of classes that could help me out I would greatly appreciate it.
Create a JPanel and override paintComponent(Graphics g). Use the Graphics object to draw your grid as well as to draw the images inside the grid. You can use ImageIO to load the images.
[QUOTE=Darwin226;21130382]Game.Services don't seem to be as straightforward as I thought. Can someone explain that and GameComponent class to me and how it applies to my problem (in one or to pages before)? Like, if I do Services.AddService(typeof(spriteBatch), spriteBatch); in Game1 class, how do I get spriteBatch from my DisplayObject class?[/QUOTE] By using GameComponent, you don't have to manually call Update and Draw from your DisplayObject class, as long as it's added to Game.Components. You can also get services from the class (such as the SpriteBatch using Game.Services.GetService(typeof(SpriteBatch));. Also, I often make an interface for each component so I can add it to services and access it from other components (I'd be happy to paste up some code showing what I mean if you don't understand it)
Ok. So I add DisplayObject to GameComponentCollecton. Does it have to have draw, update and initialize functions? Also, when I write Game., Services isn't in the dropdown menu. (That's inside my DisplayObject class) [editline]10:47AM[/editline] Yep, Services is public, not static. Only way I could access it from DisplayObject is if it extended Game class of I accessed it through the Game1 instance but since Program.cs makes the only instance of the Game1 and it's not public, or static I don't think it can be accessed from anywhere but inside the class. I'm obviously missing something important.
[QUOTE=Darwin226;21137088]Ok. So I add DisplayObject to GameComponentCollecton. Does it have to have draw, update and initialize functions? Also, when I write Game., Services isn't in the dropdown menu. (That's inside my DisplayObject class) [editline]10:47AM[/editline] Yep, Services is public, not static. Only way I could access it from DisplayObject is if it extended Game class of I accessed it through the Game1 instance but since Program.cs makes the only instance of the Game1 and it's not public, or static I don't think it can be accessed from anywhere but inside the class. I'm obviously missing something important.[/QUOTE] Are you making DisplayObject inherit GameComponent? Should look something like this [cpp] public class DisplayObject : GameComponent { SpriteBatch spriteBatch; public DisplayObject(Game game) : base(game) { // constructor stuff } public override void Initialize() { spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch)); } public override void Update(GameTime timing) { // update stuff base.Update(timing); } } [/cpp] If you need draw, make the first line: [cpp] public class DisplayObject : GameComponent, IDrawable [/cpp] and then implement the interface (VS does this for you if you click the name of the interface, mouse over the dropdown box, click and then select Implement Interface)
Ok, I finally fixed all errors (I'll also have to read up on interfaces). The problem is that the Update method isn't called every frame (or at all), what do I do so that it updates?
[QUOTE=Darwin226;21138493]Ok, I finally fixed all errors (I'll also have to read up on interfaces). The problem is that the Update method isn't called every frame (or at all), what do I do so that it updates?[/QUOTE] Is the component set to be updated? (this.Updatable in the component class)
I hate to be spoonfed but could you provide some source of a link to something that focuses on this issue? Because I Updated doesn't exist in my DisplayObject class and it does inherit from GameComponent.
[QUOTE=Darwin226;21139808]I hate to be spoonfed but could you provide some source of a link to something that focuses on this issue? Because I Updated doesn't exist in my DisplayObject class and it does inherit from GameComponent.[/QUOTE] Could you post code? I'll take a look at it for you
[cpp]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace GameEngine { class DisplayObject : GameComponent, IUpdateable { public static GraphicsDevice graphicsDevice; public static SpriteBatch spriteBatch; Texture2D textura; public Vector2 pos; public float rotation; public static List<DisplayObject> DisplayList=new List<DisplayObject>(); public DisplayObject(Game game, Texture2D atx) : base(game) { textura = atx; } public override void Initialize(){ this.Updatable = true; //no worky.... spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch)); base.Initialize(); } public override void Update(GameTime gameTime){ DisplayObject obj=this; spriteBatch.Begin(); spriteBatch.Draw(obj.textura, obj.pos, null, Color.White, obj.rotation, new Vector2(obj.textura.Width / 2, obj.textura.Height / 2), 1f, SpriteEffects.None, 0); spriteBatch.End(); } public static void drawAll() { int i = 0; while (i < DisplayList.Count) { //draw(DisplayList[i]); } } } } [/cpp]
I think it's because you're doing draw code in the Update function. Here, I've added in the IDrawable interface for you. [cpp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace GameEngine { class DisplayObject : GameComponent, IDrawable { public SpriteBatch spriteBatch; Texture2D textura; public Vector2 pos; public float rotation; public static List<DisplayObject> DisplayList=new List<DisplayObject>(); public DisplayObject(Game game, Texture2D atx) : base(game) { textura = atx; visible = true; } public override void Initialize(){ spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch)); base.Initialize(); } public override void Update(GameTime gameTime){ // Update stuff here base.Update(gameTime); } public static void drawAll() { int i = 0; while (i < DisplayList.Count) { //draw(DisplayList[i]); } } #region IDrawable Members // This is the function you're supposed to use for drawing things public void Draw(GameTime gameTime) { DisplayObject obj = this; // What's this supposed to do? spriteBatch.Begin(); spriteBatch.Draw(obj.textura, obj.pos, null, Color.White, obj.rotation, new Vector2(obj.textura.Width / 2, obj.textura.Height / 2), 1f, SpriteEffects.None, 0); spriteBatch.End(); } // Draw order is a value that determines when something is to be drawn (i.e. something with draw order 0 will be drawn before draw order 1) int drawOrder; public int DrawOrder { get { return drawOrder; } // this set bit is optional, I just put it here to show you how the DrawOrderChanged event could be fired set { drawOrder = value; if(DrawOrderChanged != null) DrawOrderChanged(this, null); } } public event EventHandler DrawOrderChanged; // Determines whether or not to draw the component at all bool visible; public bool Visible { get { return visible; } // this set bit is optional, I just put it here to show you how the Visible event could be fired set { visible = value; if (VisibleChanged != null) VisibleChanged(this, null); } } public event EventHandler VisibleChanged; #endregion } } [/cpp] The events there have something to with the way XNA handles components
Can anyone tell me why this code is crashing? [code]/* This program will launch a filled GOval from the bottom-left * corner of the graphics window towards the point clicked. The * cannonball will then bounce as normal. */ import acm.program.*; import acm.graphics.*; import java.awt.*; import java.awt.event.*; public class Cannon extends GraphicsProgram { private static final int GRAVITY = 3; private static final int X_VEL = 1; private static final int Y_VEL = 1; public static final int APP_WIDTH = 800; public static final int APP_HEIGHT = 600; public void run() { addMouseListeners(); setSize(APP_WIDTH,APP_HEIGHT); label = new GLabel(""); label.setFont("Times New Roman-36"); add(label, 50, 50); cannon = new GRect (50,50); cannon.setFilled(true); cannon.setFillColor(Color.red); add(cannon, 0, 549); } public void mouseMoved(MouseEvent e) { label.setLabel("Crosshair: (" + e.getX() + ", " + (APP_HEIGHT - e.getY()) + ")"); } public void mouseClicked(MouseEvent e) { ball = new GOval(40,540,20,20); ball.setFilled(true); ball.setFillColor(Color.blue); add(ball); ball.sendToBack(); launchBall(); } public void launchBall() { int tarX = (e.getX()); int tarY = (APP_HEIGHT - (e.getY())); moveBall(tarX,tarY); } public void moveBall(int x, int y) { ball.move(x,y); } private GLabel label; private MouseEvent e; private GRect cannon; private GOval ball; } [/code] It runs, but when I click it draws a shape and immediately crashes.
Ok, I changed the constructor so that it takes a string and makes a Texture2D inside the class. I also added Content to services (I hope it's possible, it doesn't throw any errors) It compiles fine but I only see a CornflowerBlue window. No picture or anything. P.S. Why do I want the draw order thing? I presume it's for controlling what's drawn over what, so how do I use it?
[QUOTE=tanthreecle;21140758]Can anyone tell me why this code is crashing? [code]/* This program will launch a filled GOval from the bottom-left * corner of the graphics window towards the point clicked. The * cannonball will then bounce as normal. */ import acm.program.*; import acm.graphics.*; import java.awt.*; import java.awt.event.*; public class Cannon extends GraphicsProgram { private static final int GRAVITY = 3; private static final int X_VEL = 1; private static final int Y_VEL = 1; public static final int APP_WIDTH = 800; public static final int APP_HEIGHT = 600; public void run() { addMouseListeners(); setSize(APP_WIDTH,APP_HEIGHT); label = new GLabel(""); label.setFont("Times New Roman-36"); add(label, 50, 50); cannon = new GRect (50,50); cannon.setFilled(true); cannon.setFillColor(Color.red); add(cannon, 0, 549); } public void mouseMoved(MouseEvent e) { label.setLabel("Crosshair: (" + e.getX() + ", " + (APP_HEIGHT - e.getY()) + ")"); } public void mouseClicked(MouseEvent e) { ball = new GOval(40,540,20,20); ball.setFilled(true); ball.setFillColor(Color.blue); add(ball); ball.sendToBack(); launchBall(); } public void launchBall() { int tarX = (e.getX()); int tarY = (APP_HEIGHT - (e.getY())); moveBall(tarX,tarY); } public void moveBall(int x, int y) { ball.move(x,y); } private GLabel label; private MouseEvent e; private GRect cannon; private GOval ball; } [/code] It runs, but when I click it draws a shape and immediately crashes.[/QUOTE] I don't think it should even compile: e is not defined in launchBall().
[QUOTE=ZeekyHBomb;21140843]I don't think it should even compile: e is not defined in launchBall().[/QUOTE] It's a private member field defined below. edit: Although it does look like it's not getting initialized. Instead of using the private member field, just pass it to launchBall as a function argument.
[QUOTE=Darwin226;21140776]Ok, I changed the constructor so that it takes a string and makes a Texture2D inside the class. I also added Content to services (I hope it's possible, it doesn't throw any errors) It compiles fine but I only see a CornflowerBlue window. No picture or anything. P.S. Why do I want the draw order thing? I presume it's for controlling what's drawn over what, so how do I use it?[/QUOTE] If I remember right, you can't load content properly outside of LoadContent, because something happens which means content has to be reloaded. Try passing the Texture2D again and see if it works To use draw order, you just change its value for everything you want to draw. Say you want a character over a tile, you'd set the tile to have a lower draw order value than the character, so it gets drawn first and then the character gets drawn over it.
Same thing. The problem is that the Draw function isn't called at all. [editline]06:16PM[/editline] Nor is Initialize or Update. Only thing called is the constructor.
[QUOTE=Darwin226;21142108]Same thing. The problem is that the Draw function isn't called at all. [editline]06:16PM[/editline] Nor is Initialize or Update. Only thing called is the constructor.[/QUOTE] I see. in your main game class, in initialise or the constructor, wherever you make your DisplayObject, add it to the components using this.Components.Add(<insert DisplayObject variable name here>)
Yes, they are called now. Thanks for your help. I know it might be too much but is it possible to pact that Component.Add part into the DisplayObject class? I really dislike how XNA does Drawing and I'd recode everything if I could but since I can't, I'd at least like to wrap everything in classes and methods that are easy to use. Having to use Component.Add every time I make something that needs to be displayed on screen is a pretty big issue for me.
[QUOTE=Darwin226;21142396]Yes, they are called now. Thanks for your help. I know it might be too much but is it possible to pact that Component.Add part into the DisplayObject class? I really dislike how XNA does Drawing and I'd recode everything if I could but since I can't, I'd at least like to wrap everything in classes and methods that are easy to use. Having to use Component.Add every time I make something that needs to be displayed on screen is a pretty big issue for me.[/QUOTE] You might be able to pack it, try in the constructor: Game.Components.Add(this); Also, how I handle drawing is to have a graphic manager class, a graphics manager interface, a layer interface and an object interface. A the graphics manager stores a list of layers which store lists of objects, and you can add, remove, get and set layers through the graphics manager interface (which you'd pull out of services).
Thanks for your help so far. This program opens but doesn't do anything. No exceptions, nothing. It won't even set the size of the window. [code]/* This program creates a crowd of randomly placed circles * which move and bounce off each other and the walls of the window. */ import acm.program.*; import acm.util.*; import acm.graphics.*; public class Crowd extends GraphicsProgram { public static final int APP_WIDTH = 800; public static final int APP_HEIGHT = 600; public static final int NUM_PEOPLE = 15; public static final int SIZE_PEOPLE = 20; public void main() { setup(); //startMoving(); } /* Creates the circles, one by one, at random positions, filled with random colors.*/ private void setup() { setSize(APP_WIDTH,APP_HEIGHT); for (int i = 1; i < NUM_PEOPLE; i++) { GOval person = new GOval (random.nextInt(SIZE_PEOPLE,(APP_WIDTH - SIZE_PEOPLE)), random.nextInt(SIZE_PEOPLE,(APP_HEIGHT - SIZE_PEOPLE)), SIZE_PEOPLE,SIZE_PEOPLE); person.setFilled(true); person.setFillColor(random.nextColor()); add(person); } } private RandomGenerator random; } [/code] Ideas?
Sorry, you need to Log In to post a reply to this thread.