Can someone see what's wrong in this code in GML? - game-maker

var xpos = 0;
var ypos = 0;
xpos = obj_magnet.x;
ypos = obj_magnet.y;
if (xpos > 435 && xpos < 704 && ypos > 350 && ypos < 640)
{
with(obj_lamp)
{
instance_change(obj_lamp_light, true);
}
if obj_lamp_light.image_index == 5
{
obj_lamp_light.image_speed = 0;
}
with(obj_arrow)
{
instance_change(obj_arrow_move_one_direction, true);
}
if obj_arrow_move_one_direction.image_index == 4
{
obj_arrow_move_one_direction.image_speed = 0;
}
with(obj_arrow_move_opposite_direction)
{
instance_change(obj_arrow, true);
}
exit;
}
else
{
with(obj_lamp_light)
{
instance_change(obj_lamp, true);
}
with(obj_arrow_move_one_direction)
{
instance_change(obj_arrow_move_opposite_direction, true);
if obj_arrow_move_opposite_direction.image_index == 4
{
obj_arrow_move_opposite_direction.image_speed = 0;
}
}
exit;
}
This is a game which I am trying to create where the obj_arrow_move_opposite_direction and obj_arrow_move_one_direction contains 5 sub images and the obj_lamp_light contains 5 sub images.

You stated that each object contains 5 sub-images, correct? Looks like you're looking for a nonexistent sub-image (5) on line 7.
This appears to be a simple typo, but if not it's important to note that image_index starts at 0. So 5 images would be 0-4, rather than 1-5.
var xpos=0;
var ypos=0;
xpos=obj_magnet.x;
ypos=obj_magnet.y;
if(xpos>435&&xpos<704&&ypos>350&&ypos<640) { with(obj_lamp){
instance_change(obj_lamp_light,true);} if
obj_lamp_light.image_index==5{ obj_lamp_light.image_speed=0;} // should be object_lamp_light.image_index == 4
with(obj_arrow){
instance_change(obj_arrow_move_one_direction,true);}
if obj_arrow_move_one_direction.image_index==4{
obj_arrow_move_one_direction.image_speed=0;}
with(obj_arrow_move_opposite_direction){
instance_change(obj_arrow,true);}
exit;
}
else{
with(obj_lamp_light){
instance_change(obj_lamp,true);}
with(obj_arrow_move_one_direction){
instance_change(obj_arrow_move_opposite_direction,true);
if obj_arrow_move_opposite_direction.image_index==4{
obj_arrow_move_opposite_direction.image_speed=0;}}
exit;
}
Better yet, if you're checking to make sure that the object's image_indexes are equal to the last sub-image you could use image_number and subtract by one. This way if you ever add more sub-images you could ensure your code is still checking for the last frame.

Related

I'm trying to read data from an hash table into a linked list

Im trying to read data from an global hashtable into a linked list. I cant seem to see where I went wrong. Each time I run the program, I get a runtime error.
node *locallinkedlist = NULL;
//Reading Data From the global hashtable into a local linked list to find data using binary search
for(int p = 0; p < 25; p++)
{
for(node *c = hashtable[p]; c != NULL; c = c->next)
{
if(locallinkedlist == NULL)
{
locallinkedlist = c;
}
else
{
c = locallinkedlist;
locallinkedlist = c;
}
}
}
for(node *h = locallinkedlist; h != NULL; h=h->next)
{
printf("%s",h->employeefirstname);
}
I figured it out!
I need to create a node pointer using malloc inside the inner for loop, for each employee and store each of those employee name inside the node pointer and then put it into the list. Heres my revides code!!:
node *head = NULL;
for(int i = 0; i < 25; i++)
{
for(node *j = hashtable[i]; j != NULL; j = j->next)
{
node *m = malloc(sizeof(node));
if(m == NULL)//Checking for a valid memory address
{
return 1;
}
strcpy(m->employeefirstname,j->employeefirstname);
m->next = NULL;
if(head == NULL)
{
head = m;
}
else
{
m->next = head;
head = m;
}
}
}
for(node *c = head; c != NULL; c = c->next)
{
printf("%s\n",c->employeefirstname);
}

Error: Cannot read field "color" because "u" is null RED BLACK TREE

Hello I have an assignment to implement a RBT tree and then as an application insert dictionary in it for more efficient insertion and searching operations.
When I insert the dictionary file it only reads the first word and 2nd word but doesn't fix up the insertion of the 2nd word and this error shows up enter image description here when I debugged the problem is in the insertfixup method. I think that's where the u is but I'm not sure why even if I don't initialize u with anything it gives me the same error so I tried to initialize it with TNULL and it still the same I hope my problem is clear
private void insertFixUp(Node x) {
Node u = TNULL;
while (x.parent.color == 1){
if(x.parent == x.parent.parent.right){
u=x.parent.parent.left;
if (u.color == 1){
u.color =0;
x.parent.color = 0;
x.parent.parent.color = 1;
x = x.parent.parent;
}
else {
if(x == x.parent.left) {
x = x.parent;
rightRotate(x);
}
x.parent.color=0;
x.parent.parent.color=1;
leftRotate(x.parent.parent);
}
} else {
u = x.parent.parent.right;
if (u.color == 1){
u.color =0;
x.parent.color = 0;
x.parent.parent.color = 1;
x = x.parent.parent;
}
else {
if(x == x.parent.right) {
x = x.parent;
leftRotate(x);
}
x.parent.color=0;
x.parent.parent.color=1;
leftRotate(x.parent.parent);
}
}
if(x == root){
break;
}
}
root.color=0;
}

Last line of a datatable asp.net

I have a problem when I'm trying to a loop in a DataTable that a dataset contains.
I'm doing a loop like this:
for(int i = 0; i<ds.Tables[0].Rows.Count - 1 ; i++)
The problem is that I can't get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I'll have an out of range exception.
This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:
if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])
So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it's out of the table.
So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn't work.
if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)
If anyone has an idea, let me know, and of course if it is not clear enough let me know, I'll give more information and more code.
Thanks for your help and your time!
Check if it's the last record, first.
I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.
Try to make each line of code do one thing, and one thing only, like check if it is the last row:
var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;
for(int i = 0; i < lastRow ; i++)
{
if (i == lastRow){
// This is the last row. Handle the last row here.
}
else
{
// Handle all other rows here
var currentRecord = data[i];
var nextRecord = data[i + 1];
if (currentRecord["Release_No"] != nextRecord["Release_No"])
{
// Handle unique Releases...
}
}
}
Use less than or equal to like this
for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)
I hope this may get what you want.
Something like this is better ?
var lastRow = data.Count - 1;
var data = ds.Tables[0].Rows;
for(int i = 0; i< lastRow; i++)
{
testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);
if ( i == lastRow)
{
if (DBNull.Value.Equals(data[i]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
else
{
var col = ds.Tables[0].Columns;
var currentRecord = data[i];
var nextRecord = data[i + 1];
if(currentRecord["Release_No"] != nextRecord["Release_No"])
{
for (int j = col[2].Ordinal; j < col.Count; j++)
{
if (DBNull.Value.Equals(data[i][j]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i][j]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
}
}
}

GML Timers and Random event

I'm working on a mobile game that involves a breaking wall. I have my sprites set to change on an alarm. I created an object and placed it in the room with no sprite to handle the event triggers since I thought it might be the creation code for the room that was causing problems. I.E. (Creation code was a bad place to execute)
The creation code for the object sets the alarm to 60
The code I'm using is linked to alarm[0] here:
var WallBreakVal
WallBreakVal = random_range(1, 9);
with(Wall_tiles1)
{
if WallBreakVal = 1
{
sprite_index = Wall2
}
}
with(Wall_tiles2)
{
if WallBreakVal = 2
{
sprite_index = Wall2
}
}
with(Wall_tiles3)
{
if WallBreakVal = 3
{
sprite_index = Wall2
}
}
with(Wall_tiles4)
{
if WallBreakVal = 4
{
sprite_index = Wall2
}
}
with(Wall_tiles5)
{
if WallBreakVal = 5
{
sprite_index = Wall2
}
}
with(Wall_tiles6)
{
if WallBreakVal = 6
{
sprite_index = Wall2
}
}
with(Wall_tiles7)
{
if WallBreakVal = 7
{
sprite_index = Wall2
}
}
with(Wall_tiles8)
{
if WallBreakVal = 8
{
sprite_index = Wall2
}
}
with(Wall_tiles9)
{
if WallBreakVal = 9
{
sprite_index = Wall2
}
}
alarm[0] = 5*room_speed;
The problem was the random_range function. Irandom function returns an integer instead of the entire range

Multiple Clicks Before Button Reacts

I have 3 areas set up to act like buttons in my application, they work, but for some reason I have to click it multiple times before it does what it's supposed to do. Is this because it's checking if the user has click in that specific area?
(I'm using LWJGL/Slick2D)
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
int xpos = Mouse.getX();
int ypos = Mouse.getY();
//Play Game Button
if((xpos > 276 && xpos < 556 ) && (ypos > 230 && ypos < 270)) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
click = new Sound("res/click.wav");
click.play();
TOW = new Music("res/TownOfWishes.ogg");
TOW.loop();
TOW.setVolume(0.1f);
}
}
//Exit Game Button
if((xpos > 276 && xpos < 556 ) && (ypos > 154 && ypos < 195)) {
if(Mouse.isButtonDown(0)) {
click = new Sound("res/click.wav");
click.play();
System.exit(0);
}
}
//Settings Game Button
if((xpos > 276 && xpos < 556 ) && (ypos > 80 && ypos < 121)) {
if(Mouse.isButtonDown(0)) {
click = new Sound("res/click.wav");
click.stop();
sbg.enterState(2);
}
}
}
Your code looks good but your problem could be your coordinate system(orthographic matrix setup) or your actual calculation itself. Try double checking your math.

Resources