Spawning Enemies on Game Maker - game-development

I am coding a game on game maker studio in which I want a number of enemies to be spawned randomly on my grid every time the game is run. However, I am only getting one enemy on my map each time I run it.
Below is the Create instance for my enemy object.
x_speed_ = 0;
y_speed_ = 0;
max_speed_ = 1.5;
move_speed = 1;
acceleration_ = .3;
path_start(follow_path, move_speed, continue_path, true);
Also, this is the code in the Create instance for the level that is used to place the enemies on my grid.
for(var n = 1; n < 8; n++) {
i = irandom_range(1, grid_width-2);
j = irandom_range(1, grid_height-2);
instance_create_layer(i, j, "Instances", object_enemy);
}
Can anyone spot the reason why I'm not getting multiple enemies in my level?

I can think of 2 possible scenarios in the given context:
the "Level" object is not placed in the room, so it doesn't run the said code.
the randomiser fails to be random, so it spawns 8 enemies on the exact same location.

Related

Godot Inversing selected rectangle area made up of two Vector2 objects

This seems like a really simple question but I've been at this for a couple of hours and need an outsiders perspective.
I'm migrating a start of a game to Godot from Unity.
I'm selecting an area of tiles (startDragPosition, endDragPosition, both Vector2 objects) from a TileMap and setting them to a certain tile. Currently the dragging only works if the direction is top->bottom and left->right, so if the ending x and y are larger than the starting x and y
In Unity(C#) I had a few simple lines to flip the rectangle values if it was dragged in reverse.
if (end_x < start_x) {
int tmp = end_x;
end_x = start_x;
start_x = tmp;
}
if (end_y < start_y) {
int tmp = end_y;
end_y = start_y;
start_y = tmp;
}
However in when I try a similar approach in Godot it is not working for some reason. I'm thinking that I'm messing up somewhere earlier and any help would be appreciated. If there is an easier way of doing this please tell me I'm fairly new to Godot itself.
Here is the function responsible for dragging in my Godot script(GD)
func Drag():
if(Input.is_action_just_pressed("click")):
startDragPosition=get_global_mouse_position()
if(Input.is_action_pressed("click")):
endDragPosition=get_global_mouse_position()
print("01 START: "+String(stepify(startDragPosition.x-8,16)/16)+"_"+String(stepify(startDragPosition.y-8,16)/16))
print("01 END: "+String(stepify(endDragPosition.x-8,16)/16)+"_"+String(stepify(endDragPosition.y-8,16)/16))
if(endDragPosition.x<startDragPosition.x):
var temp = endDragPosition.x
endDragPosition.x=startDragPosition.x
startDragPosition.x=temp
if(endDragPosition.y<startDragPosition.y):
var temp = endDragPosition.y
endDragPosition.y=startDragPosition.y
startDragPosition.y=temp
for x in range(startDragPosition.x,endDragPosition.x):
for y in range(startDragPosition.y,endDragPosition.y):
get_node("../DragPreview").set_cell((stepify(x-8,16))/16,(stepify(y-8,16))/16,0)
#get_node("../DragPreview").update_bitmask_area(Vector2((stepify(x-8,16))/16,(stepify(y-8,16))/16))
if(Input.is_action_just_released("click")):
print("START: "+String(stepify(startDragPosition.x-8,16)/16)+"_"+String(stepify(startDragPosition.y-8,16)/16))
print("END: "+String(stepify(endDragPosition.x-8,16)/16)+"_"+String(stepify(endDragPosition.y-8,16)/16))
startDragPosition=null
endDragPosition=null
When you drag, you always write to endDragPosition.
When you drag to the left or drag up, and you update endDragPosition, it will have smaller coordinates than it had before. Because of that you swap the coordinates with startDragPosition… And then you keep dragging left or up, and that updates endDragPosition again. The original startDragPosition is lost.
Either you work with a copy when you are deciding the start and end:
var start = startDragPosition
var end = endDragPosition
if(end.x<start.x):
var temp = end.x
end.x=start.x
start.x=temp
if(end.y<start.y):
var temp = end.y
end.y=start.y
start.y=temp
for x in range(start.x,end.x):
for y in range(start.y,end.y):
# whatever
pass
Or you forget this swapping shenanigans, and give the loops a step:
var start = startDragPosition
var end = endDragPosition
for x in range(start.x,end.x,sign(end.x-start.x)):
for y in range(start.y,end.y,sign(end.y-start.y)):
# whatever
pass

Trying To The Player Attack While Moving

I tried all the ways i found and none work please help.
I want the player to attack while moving its that simple but i cant do it for some reason. I'm new to Game Maker Language!
Create a sprite(like a dragon)
create 4 layers of that sprite(1: Stable dragon, 2: Dragon charging breath + ready to flap, 3: dragon firing + flapping, 4: dragon fired + flapped)
create an object for that sprite
create a create event and drag code
use these: image_index = 0; image_speed = 0; + in movement; gravity event( angle -90, power : 1)
Create a global left click event and use this code :
if(image_index == 0){image_speed = .3; "create a fireball etc" vspeed = 10; hspeed = 10; }
Create a Animation End event in other and use these :
image_index = 0; image_speed = 0;
this is an easy way to make a dragon which moves like a flappy bird while shooting fireballs. You can sample this to create a moving object who shoots.

(Bukkit) (Java) Is there a way to spawn random mobs within a range, for example 32 blocks from the player with a little delay?

I want to make a way to summon mobs at a rando location within a range (32 blocks from the player location) every 5 seconds for example.
And if its possible, will it cause too much lagg?
And will it be always active?
Yes, you can get their Location get their X and Z and increase by a random amount with a random, then spawn the mob. Make this in a new thread or in a bukkit runnable since otherwise yes it may cause lag (not sure about that part)
example code:
new Thread(() -> {
int newX = p.getLocation().getX() + new Random().nextInt(32)+1;
int newZ = p.getLocation().getZ() + new Random().nextInt(32)+1;
int newY = /* find out the y value of the area */
Location spawnLoc = new Location(Bukkit.getWorld("world_name"), newX, newY, newZ);
spawnMob(spawnLoc);
}).start();

Enabling/disabling CCButtons in an array

I'm building a puzzle game with 20 levels using Cocos2D. When a player first opens the game, all the levels should be locked except for level 1. When the player passes level 1, then level 2 button should be enabled and so on...
I put the CCButtons in an array so that I could disable unlocked level buttons in a for loop (rather than each individually), but enabling/disabling the object references from the array doesn't work...
For example, this works: _level19.enabled = false;
But this doesn't work: levels[19].enabled = false;
Here is the relevant code block after I initialized CCButton* _level1 to CCButton* _level20.
-(void) didLoadFromCCB {
// Only set level buttons as enabled if player has unlocked that level
CCButton* levels[20] = {_level1, _level2, _level3, _level4, _level5, _level6, _level7, _level8, _level9,
_level11, _level12, _level13, _level14, _level15, _level16, _level17, _level18, _level19, _level20};
// CCButtons are automatically enabled so disable all buttons that haven't been unlocked
for (NSInteger j = [GameState sharedInstance].levelsUnlocked; j > 20; j++) {
levels[j].enabled = false;
}
}
Any help would be appreciated!
j > 20 seems like a typo. You want to get inside the loop only if your j value is greater than 20 , and then fetch a value from levels array of count 20 which would throw an out of bounds exception.
I am guessing, this should work for you
for (NSInteger j = [GameState sharedInstance].levelsUnlocked; j < 20; j++) {
levels[j].enabled = false;
}

Actionscript 3: How to make movieclip variables work with variables on stage (Healthbar Help)

I am totally new at this whole programming thing, and I really need help. So basically, I'm trying to make a healthbar that will increase or decrease depending on what button is clicked. I made a healthbar movieclip with 101 frames (I included zero) and put this in the actionscript layer of the movieclip:
var health:Number = 0;
if(health == 0)
{
gotoAndStop("1")
}
if(health == 1)
{
gotoAndStop("2")
}
if(health == 2)
{
gotoAndStop("3")
}
and on and on like so. Basically, on the stage itself, I have a button called fortyfiveup_btn that is commanded to do this:
var health:Number = 0;
fortyfiveup_btn.addEventListener(MouseEvent.CLICK, fortyfiveupClick);
function fortyfiveupClick(event:MouseEvent):void{
health = health+45
}
I quickly realized that both health variables, the one for the button and the one for the healthbar will not interact. How can I make it so if the button is clicked, the health goes to the relevant frame or percentage?
Thanks for any answers, and I appreciate all the help I can get :)
If the answer == yes to my comment you should do this:
You need to give the movieclip an instancename (perhaps lifebar) and from stage you can access the health inside the "lifebar" with lifebar.health.
So you need this inside your stage:
//You can delete the var health:Number = 0;
fortyfiveup_btn.addEventListener(MouseEvent.CLICK, fortyfiveupClick);
function fortyfiveupClick(event:MouseEvent):void{
//You can write this, too: lifebar.health += 45;
lifebar.health = lifebar.health+45;
}
You can even optimize your lifebar script, don't use 101 times the if(health == x) you can use this, too:
gotoAndStop(health + 1);
(I think this is inside an ENTER_FRAME event?)
EDIT:
Some error countermeasures:
//Don't let health decrease below 0
if(health < 0) health = 0;
//And don't above 100
else if(health > 100) health = 100;
gotoAndStop(health + 1);
Use int instead of Number when you don't use decimal numbers and uint when you don't use negative integers (this bugs when the number can drop under 0, so for your health we take int):
health:int = 0;