Jump to content


Photo

Object moves with view...


  • Please log in to reply
8 replies to this topic

#1 tatman87

tatman87

    GMC Member

  • New Member
  • 11 posts
  • Version:GM8

Posted 07 August 2012 - 06:16 AM

Let's say an objects instance is placed randomly on the view port of the screen...
x = random(view_xview)
y = random(view_yview)

How can I get that instance to move with the view port with the same x and y values?
Thanks in advance!
  • 0

#2 Blake

Blake

    GMC Member

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

Posted 07 August 2012 - 06:36 AM

Firstly, the code you posted won't randomly place the instance within the view. To do that, you can use this:

x = view_xview[0] + irandom(view_wview[0]);
y = view_yview[0] + irandom(view_hview[0]);
To get the instance to stick to that relative point from then on, you will need to store the randomised section above in a variable and add that to the current view co-ordinates like this:

// This should only be run once, most likely in the create event.
x_amount = irandom(view_wview[0]);
y_amount = irandom(view_hview[0]);

// STEP EVENT
x = view_xview[0] + x_amount;
y = view_yview[0] + y_amount;

Edited by Blake, 07 August 2012 - 06:38 AM.

  • 0

#3 tatman87

tatman87

    GMC Member

  • New Member
  • 11 posts
  • Version:GM8

Posted 07 August 2012 - 08:27 AM

What if my instance isn't placed randomly on creation?
For example, let's say I click in the view port and the instance is moved to the cursor location. How do I capture that variable?
I can't use
x_amount = x
because that will record the x position in the room and not the view.
I can't use
x_amount = view_wview
because that will record the view width (in my case 1280)

In the end result I agree that the code should look like this...
x = view_xview[0] + x_amount
y = view_yview[0] + y_amount

but how do I record x_amount and y_amount?
  • 0

#4 Blake

Blake

    GMC Member

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

Posted 07 August 2012 - 09:34 AM

In that case, the variables will need to store the difference between the mouse co-ordinates and the view co-ordinates, like this:

x_amount = mouse_x - view_xview[0];
y_amount = mouse_y - view_yview[0];

  • 0

#5 tatman87

tatman87

    GMC Member

  • New Member
  • 11 posts
  • Version:GM8

Posted 07 August 2012 - 10:12 AM

I'm sorry to drag this out any further... but what if the instance is placed at the mouse co-ordinates and move_snap(64,64) changes the position making mouse_x/y "useless?"

Also, while I have your attention what is the difference between irandom( ) and random( ) ... ?
Is irandom in whole integers and random in decimals?

Thanks again.
  • 0

#6 Blake

Blake

    GMC Member

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

Posted 07 August 2012 - 10:35 AM

I'm sorry to drag this out any further... but what if the instance is placed at the mouse co-ordinates and move_snap(64,64) changes the position making mouse_x/y "useless?"

Then you would use the x and y position of the instance in place of mouse_x and mouse_y.

Is irandom in whole integers and random in decimals?

This is correct.
  • 0

#7 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4705 posts
  • Version:GM8

Posted 07 August 2012 - 04:12 PM

The x and y never change until you move them.

x=view_xview+irandom(view_wview) is still x=x. The variable view_xview is still an x. It's just x of the view. If the view was technically an object, view_xview would simply be view.x. So x is x. You place the object at a specific point (x,y) and then add the view's motion to it. Not sure how the view speed is handled in GM, but there should be a variable you can add to x and one you can add to y. Otherwise when you create the object you can put

xstart=xstart-view_xview;
ystart=ystart-view_yview;

And then in the object's End Step Event put

x+=xstart-(x-view_xview);
y+=ystart-(y-view_yview);
  • 0

#8 tatman87

tatman87

    GMC Member

  • New Member
  • 11 posts
  • Version:GM8

Posted 07 August 2012 - 10:53 PM

Maybe I should explain exactly what i'm trying to do instead of taking shortcuts.
I'm trying to create an inventory that is always in the view port. Most would use an array, i'm not against arrays but feel that is too advanced for me yet.
So the way this inventory works, it is the top left corner of the view port and any instance placed inside is in the inventory (literally.)
For example:
// step event

if x < view_xview + 128 && y < view_yview +128
{
move_snap(64,64);
var_inv = true
}

The above code creates a 4 slot inventory, each slot containing 64x64 space.
To put something in the inventory you would drag and drop it.
Altogether looks like this...
// create event

var_drag = false
var_inv = false

// step event

if var_drag = true
{
x = mouse_x
y = mouse_y
}

if x < view_xview + 128 && var_drag = false
{
move_snap(64,64);
var_inv = true;
}

// left mouse pressed
if var_drag = false
{
var_drag = true
}
else
var_drag = false

So far this works but the code is incomplete because the instance will not move with the view.
In theory I could do this...
if x = view_xview + 32
else
if x = view_xview + 64
else
if x = view_xview + 128
else
if x = view_xview + 196
else
then {x = bla bla bla}

But that's not a very clean code and would become clutter especially in my final game when the inventory has 10+ spaces.

Edited by tatman87, 07 August 2012 - 10:58 PM.

  • 0

#9 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4705 posts
  • Version:GM8

Posted 08 August 2012 - 12:01 AM

Try what I posted. In theory it should work.
(and yes, you can edit xstart and ystart without ruining the game as long as you never use xstart or ystart for that object anywhere else)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users