Click & Drag Mechanism - game-maker

I need help with GameMaker Studio 2 v2.3.2.556 project.
My clicking & dragging mechanism has two objects: an object called obj_iron that is being dragged, and an object called obj_cursor that is invisible and always follows the mouse. When I use the cursor to drag the iron, nothing happens. How do I make it so I can drag the iron using the cursor?
Code in obj_cursor in the Step Event
//Making the cursor move toward the mouse
global.picked_up_iron = 0
x = mouse_x
y = mouse_y
//Making the cursor small
image_xscale = 0.1
image_yscale = 0.1
//Checking if the cursor collided with obj_iron
if place_meeting(x,y,obj_iron)
{
//Checking if the mouse is held and the cursor is not collided with another obj_iron
if mouse_check_button(mb_left) and (global.picked_up_iron == 0 or global.picked_up_iron == other)
{
//Moving the dragged piece of obj_ironto the obj_cursor's location
var picked_up_iron = other
picked_up_iron.x = x - picked_up_iron.sprite_width / 2
picked_up_iron.y = y - picked_up_iron.sprite_height / 2
//Telling every the object what the dragged piece of obj_iron is
global.picked_up_iron = picked_up_iron
}
}
else if !mouse_check_button(mb_left)
{
//Resetting the value of the current piece of obj_iron
global.picked_up_iron = 0
}

If there are multiple of the same object in the same room, then place_meeting() does not know which kind of object is selected.
Maybe you can try out Instance_Place
var iron = instance_place(x, y, obj_iron);
This way, the iron returns the object the cursor is currently colliding with.

Related

Can objects be linked in Gamemaker?

Within the game I'm currently building there's an interactive element that is supposed to work the same way when interacting with different elements. To make it easier to replicate numerous times, I duplicated the objects containing this code and changed the global.variable which is supposed to define each individual 'group' (two objects make up the element itself) so they work separately - however when I interact with one group in the game, it still triggers the other one to react at the same time.
Can objects be linked in gamemaker? And if so, how do I unlink these objects so that they work separately?
Here's the code for both in case it's more an issue with something in the code that I've overlooked. Basically how the group works is that when the player interacts with the object, the object changes it's instance and the new object picks up the functionality of the old one. First three image in each group refer to the first object, last three refer to the second.
Group 1 Object 1;
Create Event
global.ssscollission = 0;
Key Press E
if (global.ssscollission = 0)
{
global.ssscollission = 1;
}
else
{
global.ssscollission = 0;
}
instance_change(obj_snakeplush,false)
Draw GUI
If Collision, Stop Movement, Font Text and Draw Value DnD features here
.
Group 1 Object 2;
Draw GUI
If Collision, Stop Movement, Font Text and Draw Value DnD features here, as well as;
if (global.ssscollission)
{
draw_set_color(c_black);
draw_set_alpha(0.75);
draw_rectangle(0,0,room_width,room_height,0);
draw_set_alpha(1);
draw_set_color(c_white);
draw_set_halign(fa_center);
draw_sprite_ext(spr_sss,0,920,50,0.65,0.65,0,c_white,1);
draw_set_font(fnt_credit);
draw_text(1275,1400,"Press the [E] button to return.")
}
Key Press E
if (global.ssscollission = 0)
{
global.ssscollission = 1;
}
else
{
global.ssscollission = 0;
}
Animation End
image_index = image_number - 1;
image_speed = 0;
global.danielfound = global.danielfound + 1
.
Group 2 Object 1;
Create Event
global.danieltree = 0;
Key Press E
if (global.danieltree = 0)
{
global.danieltree = 1;
}
else
{
global.danieltree = 0;
}
instance_change(obj_danieltree,false)
Draw GUI
If Collision, Stop Movement, Font Text and Draw Value DnD features here
.
Group 2 Object 2;
Draw GUI
If Collision, Stop Movement, Font Text and Draw Value DnD features here, as well as;
if (global.danieltree)
{
draw_set_color(c_black);
draw_set_alpha(0.75);
draw_rectangle(0,0,room_width,room_height,0);
draw_set_alpha(1);
draw_set_color(c_white);
draw_set_halign(fa_center);
draw_sprite_ext(spr_sss,0,920,50,0.65,0.65,0,c_white,1);
draw_set_font(fnt_credit);
draw_text(1275,1400,"Press the [E] button to return.")
}
Key Press E
if (global.danieltree= 0)
{
global.danieltree= 1;
}
else
{
global.danieltree= 0;
}
Animation End
image_index = image_number - 1;
image_speed = 0;
global.danielfound = global.danielfound + 1
Cheers

Qt3D point and line picking?

I've been trying to do point and line picking on a mesh in Qt3D. Here's a working version of triangle picking,
auto renderSettings = new Qt3DRender::QRenderSettings(root);
renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
auto entity = new Qt3DCore::QEntity(root);
auto picker = new Qt3DRender::QObjectPicker;
auto sphere = new Qt3DExtras::QSphereMesh;
entity->addComponent(picker);
entity->addComponent(sphere);
connect(picker, &Qt3DRender::QObjectPicker::clicked, []
(Qt3DRender::QPickEvent* e) {
auto p = dynamic_cast<Qt3DRender::QPickTriangleEvent*>(e);
auto idx = p->triangleIndex();
});
However, when I switched to point picking or line picking, the clicked event is never triggered. Any idea how to make this work? Thanks in advance.

How to bring vertex to the front in JGraphX

I am using JGraphX, and I have a vertex with 3 ports, but I want the vertex to be to the front, for some reason it is not bringing the vertex to the front, what could I be missing?
final int PORT_DIAMETER = 20;
final int PORT_RADIUS = PORT_DIAMETER / 2;
mxGeometry geo1 = new mxGeometry(0, 0.5, PORT_DIAMETER,
PORT_DIAMETER);
// Because the origin is at upper left corner, need to translate to
// position the center of port correctly
geo1.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
geo1.setRelative(true);
geo1.setWidth(10);
mxCell port1 = new mxCell(null, geo1,
"port;image=/Images/blue-right-arrow.png");
port1.setVertex(true);
mxGeometry geo2 = new mxGeometry(1.0, 0.5, PORT_DIAMETER,
PORT_DIAMETER);
geo2.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
geo2.setRelative(true);
mxCell port2 = new mxCell(null, geo2,
"port;image=/Images/blue-right-arrow.png");
port2.setVertex(true);
mxGeometry geo3 = new mxGeometry(0.5, 1, PORT_DIAMETER,
PORT_DIAMETER);
geo3.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
geo3.setRelative(true);
mxCell port3 = new mxCell(null, geo3,
"port;image=/Images/blue-up-arrow.png");
port3.setVertex(true);
graph.addCell(port1, this);
graph.addCell(port2, this);
graph.addCell(port3, this);
graph.cellsOrdered(new Object[]{port1,port2,port3}, true);
graph.cellsOrdered(new Object[]{this}, false);
I am not sure whether this is possible: the ports have a parent p1 (in your case some 'this') and p1 has a parent e.g. p2 (or the default parent). when you bring to front or back a cell you just alter the index of the cell on the same parent. In your case, if you change the parent of the ports, you will loose them because of the relative geometry (that is necessary).
You can verify this in the example GraphEditor class: create a large rectangle, then right click on the rectangle->shape->ender group, then create a second shape, then right click shape->exit group. Then try to bring to back the second shape: right click on to the shape shape->to back. Nothing changes.
One solution, if it suits you, is to create another cell on top of your 'this' cell (the one with the ports):
double w = this.getGeometry().getWidth();
double h = this.getGeometry().getHeight();
mxGeometry geo4 = new mxGeometry(0, 0, w, h);
geo4.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
String s = this.getStyle(); //set the same style
mxCell c = new mxCell(null, geo4, style);
c.setVertex(true);
graph.addCell(c, this); //set this to be the patern
Another solution is to edit your blue-right-arrow.png (port) and paint the half of it with the style of your parent vertex, in order to create the illusion that they are at the back.
Hope it helps.

Placing a floating info panel

I'm using Jame Ferreira's Google Script: Enterprise Application Essentials to create a product page (Chapter 5). I've created the app that displays thumbnails of the products all on a page, and then the info panel with more detail that pops up when the user mouses over. It's using CSS to achieve this styling.
Problem comes in when placing the info panel. It pops up in only one location, as opposed to being tied to the thumbnail with which it is associated.
Here's the code related to the info panel. I believe the problem exists in the placement of the last few lines, but have tried everything I can think of:
function onInfo(e){
var app = UiApp.getActiveApplication();
var id = e.parameter.source;
for (var i in productDetails){
if(productDetails[i].id == id){
var r = 50;
var c = 0;
var infoPanel = app.createVerticalPanel().setSize('300px', '300px');
var horzPanel = app.createHorizontalPanel();
var image = app.createImage(productDetails[i].imageUrl).setHeight('100px');
var title = app.createLabel(productDetails[i].title);
horzPanel.add(image);
horzPanel.add(title);
var description = app.createLabel(productDetails[i].description);
infoPanel.add(horzPanel);
infoPanel.add(description);
applyCSS(infoPanel, _infoPanel);
applyCSS(image, _infoImage);
applyCSS(horzPanel, _infoBoxSeparator);
applyCSS(title, _infoTitle);
applyCSS(description, _infoDescription);
app.getElementById('infoGrid').setVisible(true)
.setWidget(0,0, infoPanel);
break;
}
c++
if (c == 3){
c = 0;
r = r+250;
}
switch (c)
{
case 0:
c=250;
break;
case 1:
c=500;
break;
case 2:
c=250;
break;
}
}
var _infoLocation =
{
"top":r+"px",
"left":c+"px",}
applyCSS(infoPanel, _infoLocation);
return app;
}
You may get the position of the mouse using e.parameter.x and e.parameter.y based on which you can position the infoPanel
infoPanel.setStyleAttribute('top',e.parameter.y)
.setStyleAttribute('left',e.parameter.x).setStyleAttribute('zIndex','1')
.setStyleAttribute('position','fixed');
You may add some offset to x and y positions of mouse to display it according to your needs.
Position related parameter which you get in handler function are
e.parameter.x
e.parameter.y
e.parameter.clientX
e.parameter.clientY
e.parameter.screenX
e.parameter.screenY

Zoom into group of points in Flex

I have an application in Flex 4 with a map, a database of points and a search tool.
When the user types something and does the search it returns name, details and coordinates of the objects in my database.
I have a function that, when i click one of the results of my search, it zooms the selected point of the map.
The question is, i want a function that zooms all the result points at once. For example if i search "tall trees" and it returns 10 points, i want that the map zooms to a position where i can see the 10 points at once.
Below is the code im using to zoom one point at a time, i thought flex would have some kind of function "zoom to group of points", but i cant find anything like this.
private function ResultDG_Click(event:ListEvent):void
{
if (event.rowIndex < 0) return;
var obj:Object = ResultDG.selectedItem;
if (lastIdentifyResultGraphic != null)
{
graphicsLayer.remove(lastIdentifyResultGraphic);
}
if (obj != null)
{
lastIdentifyResultGraphic = obj.graphic as Graphic;
switch (lastIdentifyResultGraphic.geometry.type)
{
case Geometry.MAPPOINT:
lastIdentifyResultGraphic.symbol = objPointSymbol
_map.extent = new Extent((lastIdentifyResultGraphic.geometry as MapPoint).x-0.05,(lastIdentifyResultGraphic.geometry as MapPoint).y-0.05,(lastIdentifyResultGraphic.geometry as MapPoint).x+0.05,(lastIdentifyResultGraphic.geometry as MapPoint).y+0.05,new SpatialReference(29101)).expand(0.001);
break;
case Geometry.POLYLINE:
lastIdentifyResultGraphic.symbol = objPolyLineSymbol;
_map.extent = lastIdentifyResultGraphic.geometry.extent.expand(0.001);
break;
case Geometry.POLYGON:
lastIdentifyResultGraphic.symbol = objPolygonSymbol;
_map.extent = lastIdentifyResultGraphic.geometry.extent.expand(0.001);
break;
}
graphicsLayer.add(lastIdentifyResultGraphic);
}
}
See the GraphicUtil class from com.esri.ags.Utils package. You can use the method "getGraphicsExtent" to generate an extent from an array of Graphics. You then use the extent to set the zoom factor of your map :
var graphics:ArrayCollection = graphicsLayer.graphicProvider as ArrayCollection;
var graphicsArr:Array = graphics.toArray();
// Create an extent from the currently selected graphics
var uExtent:Extent;
uExtent = GraphicUtil.getGraphicsExtent(graphicsArr);
// Zoom to extent created
if (uExtent)
{
map.extent = uExtent;
}
In this case, it would zoom to the full content of your graphics layer. You can always create an array containing only the features you want to zoom to. If you find that the zoom is too close to your data, you can also use map.zoomOut() after setting the extent.
Note: Be careful if you'Ve got TextSymbols in your graphics, it will break the GraphicUtil. In this case you need to filter out the Graphics with TextSymbols
Derp : Did not see the thread was 5 months old... Hope my answer helps other people

Resources