Jump to content


Photo

Need help with GML!


  • Please log in to reply
17 replies to this topic

#1 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 22 February 2012 - 06:36 PM

I have been having problems trying to figure out how to shoot in a particular direction at the proper sub image.

What i want is :-
When i right click my character, I want it to play the attack animation and at the end of the animation i want it to create a fireball.

Object8 = Fireball
Mage = The character.

if global.facing="R"
{
sprite_index = MAright
image_index += 1
if image_index = 10
{
instance_create(Mage.x,Mage.y,object8)
motion_set(180,6)
}
sprite_index = Mright
}

It spawns the fireball but doesnt play the animation properly. Help please.
  • 0

#2 DivideBy2

DivideBy2

    GMC Member

  • New Member
  • 64 posts
  • Version:GM8

Posted 22 February 2012 - 06:41 PM

I have been having problems trying to figure out how to shoot in a particular direction at the proper sub image.

What i want is :-
When i right click my character, I want it to play the attack animation and at the end of the animation i want it to create a fireball.

Object8 = Fireball
Mage = The character.

if global.facing="R"
{
sprite_index = MAright
image_index += 1
if image_index = 10
{
instance_create(Mage.x,Mage.y,object8)
motion_set(180,6)
}
sprite_index = Mright
}

It spawns the fireball but doesnt play the animation properly. Help please.

I have never used motion_set so I'm not sure about that, but is this happening inside the mage step event? If it is, wouldn't motion_set set the motion of the mage and not the fireball?
  • 0

#3 AKSuperNewb

AKSuperNewb

    GMC Member

  • GMC Member
  • 140 posts

Posted 22 February 2012 - 07:09 PM

You need to change the instance_change and motion_set lines to this:
var i;
i = instance_create(Mage.x,Mage.y,object8);
with i {
     motion_set(180,6);
}

EDIT:
Alternately, you could do this (which should be faster because motion_set takes into account current motion as well - of which there is none)
var i;
i = instance_create(Mage.x,Mage.y,object8);
i.speed = 6;
i.direction = 180;

Edited by AKSuperNewb, 22 February 2012 - 07:10 PM.

  • 0

#4 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 22 February 2012 - 07:09 PM

[/quote]
I have never used motion_set so I'm not sure about that, but is this happening inside the mage step event? If it is, wouldn't motion_set set the motion of the mage and not the fireball?
[/quote]

True that! the fireballs motion is fixed but the animation is still not playing :S
  • 0

#5 DivideBy2

DivideBy2

    GMC Member

  • New Member
  • 64 posts
  • Version:GM8

Posted 22 February 2012 - 07:12 PM

How about changing the sprite to the MAright sprite and then using the animation end event.
In the event you'd put something that would first check if the sprite was the MAright sprite, then it would create a fireball and then it would change you back to the Mright sprite.
  • 0

#6 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 22 February 2012 - 07:16 PM

You need to change the instance_change and motion_set lines to this:

var i;
i = instance_create(Mage.x,Mage.y,object8);
with i {
motion_set(180,6)
}


The animation still doesnt play :(
  • 0

#7 AKSuperNewb

AKSuperNewb

    GMC Member

  • GMC Member
  • 140 posts

Posted 22 February 2012 - 07:19 PM

You are instantly setting the sprite_index from MAright to Mright. There is no delay between lines 3 and 10 of your code. Try what DivideBy2 said. If you want the fireball to spawn at the beginning of the animation, just move the fireball spawning code from the animation end event to whatever event triggers the whole thing.
EDIT: It's also possible you meant to simply put the sprite_index = Mright code in the "if" block. I'm pretty sure that would make it work as well, since that seems to be our intention. i.e.,
if global.facing="R"
{
sprite_index = MAright
image_index += 1
if image_index = 10
{
instance_create(Mage.x,Mage.y,object8)
motion_set(180,6)
sprite_index = Mright
}
}

Edited by AKSuperNewb, 22 February 2012 - 07:21 PM.

  • 0

#8 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 22 February 2012 - 07:33 PM

^Thanks alot! that worked like a charm! ty all!

Except it doesnt shoot the fireball when its standing still :/
Heres my code now :

if mouse_check_button(mb_right)

if sprite_index = Mright
{
hspeed = 0
sprite_index = MAright
image_index += 0.5
if image_index = 10
{
instance_create(Mage.x,Mage.y,object8)
motion_set(360,6)
sprite_index = Mright
}
}

Edited by _255240, 22 February 2012 - 07:36 PM.

  • 0

#9 DivideBy2

DivideBy2

    GMC Member

  • New Member
  • 64 posts
  • Version:GM8

Posted 22 February 2012 - 07:42 PM

^Thanks alot! that worked like a charm! ty all!

Except it doesnt shoot the fireball when its standing still :/
Heres my code now :

if mouse_check_button(mb_right)

if sprite_index = Mright
{
hspeed = 0
sprite_index = MAright
image_index += 0.5
if image_index = 10
{
instance_create(Mage.x,Mage.y,object8)
motion_set(360,6)
sprite_index = Mright
}
}


What sprite is the mage when he's standing still? Because if the sprite isn't Mright while you're standing still (i.e. the sprite might be "Midle" or something) then the code would check:
Right mouse pressed? -Check
Sprite = Mright? -Nope! I shan't continue!

Oh, btw, motion_set(360, 6) is the same as motion_set(0, 6) and using 0 is sexier!

Edited by DivideBy2, 22 February 2012 - 07:43 PM.

  • 0

#10 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 22 February 2012 - 07:43 PM


^Thanks alot! that worked like a charm! ty all!

Except it doesnt shoot the fireball when its standing still :/
Heres my code now :

if mouse_check_button(mb_right)

if sprite_index = Mright
{
hspeed = 0
sprite_index = MAright
image_index += 0.5
if image_index = 10
{


instance_create(Mage.x,Mage.y,object8)
motion_set(360,6)
sprite_index = Mright
}
}


What sprite is the mage when he's standing still? Because if the sprite isn't Mright while you're standing still (i.e. the sprite might be "Midle" or something) then the code would check:
Right mouse pressed? -Check
Sprite = Mright? -Nope! I shan't continue!


The image of the sprite standing still is the sub image of him walking.

Edited by _255240, 22 February 2012 - 07:43 PM.

  • 0

#11 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 23 February 2012 - 10:40 AM

HELP ME!
  • 0

#12 DivideBy2

DivideBy2

    GMC Member

  • New Member
  • 64 posts
  • Version:GM8

Posted 23 February 2012 - 01:17 PM

I think the best thing to do is to make a variable and call it something like "isattacking", and do something like:
if mouse_check_button(mb_right) && sprite_index = Mright && !isattacking
    isattacking = 1;

if isattacking {
    hspeed = 0;
    sprite_index = MAright;
    image_index += 0.5;
    
    if image_index >= 10 {
        instance_create(Mage.x, Mage.y, object8);
        motion_set(360, 6);
        sprite_index = Mright;
        isattacking = 0;
    }
}
Not sure if it'll fix your problem, but it's better this way anyway.
Also here's just a quick extra tidbit of information:
It's good practise to start all your sprite names with spr_ and object names with obj_
So you can do spr_mage and obj_mage, and you'd know what type of item it is.
Also, you might want to rename your fireball object to something more obvious like obj_fireball
It's not required, but it's just something I recommend doing.

Edited by DivideBy2, 23 February 2012 - 01:20 PM.

  • 0

#13 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 24 February 2012 - 01:42 PM

Most of the problems have been solved except that when I right click and the character is facing left, it'll attack then turn right for some reason. Going to post my entire code here. (Something is up with the movement and the projectile spawning code, its conflicting as far as I'm concerned)

MOVEMENT CODE :

hspeed = (keyboard_check(vk_right)-keyboard_check(
vk_left))*3;
if keyboard_check_pressed(vk_right)
{
sprite_index = Mright
}
if hspeed = 0 and isattacking = 0
{ image_index=0 }


hspeed = (keyboard_check(vk_right)-keyboard_check(
vk_left))*3;
if keyboard_check_pressed(vk_left)
{
sprite_index = Mleft
}
if hspeed = 0 and isattacking = 0
{ image_index=0 }


PROJECTILES

if mouse_check_button(mb_right)and sprite_index = Mright and !isattacking
    isattacking = 1;

if isattacking {
    hspeed = 0;
    sprite_index = MAright;
    image_index += 0.5;
    
    if image_index >= 10 {
        instance_create(Mage.x, Mage.y, fireball);
        isattacking = 0;
        
    }sprite_index = Mright;
}

if mouse_check_button(mb_right) and sprite_index = Mleft and !isattacking
    isattacking = 1;

if isattacking {
    hspeed = 0;
    sprite_index = MAleft;
    image_index += 0.5;
    
    if image_index >= 10 {
       i=instance_create(Mage.x, Mage.y, fireball)
        isattacking = 0;
        
    }sprite_index = Mleft;
}

Edited by _255240, 24 February 2012 - 02:02 PM.

  • 0

#14 111Studio

111Studio

    GMC Member

  • GMC Member
  • 258 posts
  • Version:GM:Studio

Posted 24 February 2012 - 05:47 PM

Okay... you've got a couple things that can be changed.

First of all your movement code can be greatly cut down.

hspeed = (keyboard_check(vk_right)-keyboard_check(
vk_left))*3;
if (hspeed>0){sprite_index = Mright;}
if (hspeed<0){sprite_index = Mleft;}

if hspeed = 0 and isattacking = 0
{ image_index=0 }

Secondly the problem in your firing code is the "isattacking" variable

if mouse_check_button(mb_right)and sprite_index = Mright and !isattacking
    isattacking = 1;

if isattacking{
if (sprite_index == Mright || sprite_index == MAright){
    hspeed = 0;
    sprite_index = MAright;
    image_index += 0.5;
    
    if image_index >= 10 {
        instance_create(Mage.x, Mage.y, fireball);
        isattacking = 0;
sprite_index = Mright; //This code was after the end bracket of the if statement...thus was ending the attack sequence before was fully shown
        
    }
}
}

if mouse_check_button(mb_right) and sprite_index = Mleft and !isattacking
    isattacking = 1;

if isattacking {
if (sprite_index == Mleft || sprite_index == MAleft){
    hspeed = 0;
    sprite_index = MAleft;
    image_index += 0.5;
    
    if image_index >= 10 {
       i=instance_create(Mage.x, Mage.y, fireball)
        isattacking = 0;
        sprite_index = Mleft;
    }
}
}

That should fix the issue.
  • 0

#15 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 24 February 2012 - 05:59 PM

Thanks alot sir!! That worked totally!! ty soooooo much!

But while your here can you help me with another issue?

I'd like my character to move where its clicked, only on the X-axis. I only want it to move left and right and change attack and moving sprites appropriately.
Coding is not necessary as I'd like to learn. If coding just put some comments here and there so my brain doesnt break.. Thankyou!

Edit :

Marker is the object to which my character should go to on the x axis. When it gets near to the marker, the marker should be destroyed and the speed set to 0. this is what i came up with:

if instance_exists(marker)
{
move_towards_point(marker.x,y,3);
}
  if point_distance(x,y,marker.x,y)<10 
        with (marker)
        {
            instance_destroy()
        };

I want it the object marker to be destroyed when the x axis of the Player and the marker are same. So far havent been very successful! Need help!

Edited by _255240, 26 February 2012 - 07:23 PM.

  • 0

#16 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 26 February 2012 - 07:25 PM

Bump!
  • 0

#17 111Studio

111Studio

    GMC Member

  • GMC Member
  • 258 posts
  • Version:GM:Studio

Posted 27 February 2012 - 01:51 AM

I would recommend not using an object for your target location. Just use a variable.

So when the mouse clicks, set a variable called targetX to the mouse_x variable.

Then in the step event:

if (point_distance(x,y,targetX,y) > hspeed){
move_towards_point(targetX,y,3);
}

If you have a question about how the code works, let me know =)

EDIT: actually you wouldn't need the point_distance function

if (abs(x-targetX) > hspeed){
move_towards_point(targetX,y,3);
}

That's probably going to be more efficient code although it doesn't really matter. lol

Edited by 111Studio, 27 February 2012 - 01:58 AM.

  • 0

#18 _255240

_255240

    GMC Member

  • New Member
  • 64 posts

Posted 27 February 2012 - 10:55 AM

^ Ive got the movement part down, just want the object marker to be destroyed when the distance between their x value is less than 10

if distance_to_object(marker)<1
        with (marker)
        {
            instance_destroy()
        };

got it to work :)

Edited by _255240, 27 February 2012 - 11:15 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users