Help with collisions and game creation


(jamal) #1

I’m new user on Hype and I make a game on it, but I have problem.
1_What I need is, if the ‘Main Car’ touches or goes out of the Right or Left-Line start a (X) Scene.
2_If the ‘Main Car’ touches another car, start a (X)Scene.

Thanks in advance!!


#2

I think a question we need to ask before getting too deep into the weeds with this is: are you planning on creating a game in JavaScript (and Hype) without knowing JavaScript?

Here’s a great search term to start with so you can see what this type of interactivity entails: Search results for ‘collision’ - Tumult Forums

There’s few built in tools for creating this type of game in Hype but knowing more about what you’re trying to build will be super helpful.


(jamal) #3

Thank you for your answer
I#m in the first semester in the university and I’m working on term paper (project). That project is to design a game on Hype.
In this project we are free to use any sources.
The Problem:
I don’t have any background on JavaScript!!
You can download the Hype file from hier and see it:

https://drive.google.com/file/d/1bq9Vng1Y0x1VDrG6fXkpMm2CS8XVM8gj/view?usp=sharing

Thanks in advance


(Hans-Gerd Claßen) #4

without javascript knowledge there’s no way …


(Mark Hunte) #5

One thing you may want to workout is your road movement as shown here… :wink:


(Hans-Gerd Claßen) #6

Hi @sivic,

i guess i missunderstood your approach … you just want the collisions to happen … right?

well they do actually not occur because your shifting the car with setting the style-property in your js.
But hypes runtime will not know of the new positions …

the solution is to use hypes own API to set the new position.

note: i changed the id for the red car to ‘car’ and gave an id to the road of ‘road’ so the car will stay within …

var keyCodes = { left: 37, up: 38, right: 39, down: 40 },
keys = [],

car = hypeDocument.getElementById('car'),
carWidth = hypeDocument.getElementProperty(car, 'width'),

road = hypeDocument.getElementById('road'),
leftStop = hypeDocument.getElementProperty(road, 'left'),
rightStop = leftStop + hypeDocument.getElementProperty(road, 'width') - carWidth;


window.addEventListener('keydown', function (evt) {
    keys[evt.keyCode] = true;
});

window.addEventListener('keyup', function (evt) {
    keys[evt.keyCode] = false;
});

setInterval(function () {
    
    // get position of div
    var x = parseInt(hypeDocument.getElementProperty(car, 'left'), 10),
        y = parseInt(hypeDocument.getElementProperty(car, 'top'), 10);
    
    // update position
    // left/right
    if (keys[keyCodes.left] && x > leftStop) {
        x -= 1;
    } else if (keys[keyCodes.right] && x < rightStop) {
        x += 1;
    }
    // up/down
    if (keys[keyCodes.up]) {
        y -= 1;
    } else if (keys[keyCodes.down]) {
        y += 1;
    }
    
    // set div position
    hypeDocument.setElementProperty(car, 'left', x);
    hypeDocument.setElementProperty(car, 'top', y);
    
}, 1/30);

keep on! :slight_smile:


(jamal) #7

Thanks from deep of my heart man it’s work perfectly.
You are Awesome guys!!
I have one more question pls
how can I start or move to another scenes if the main car collides with another car?


(Hans-Gerd Claßen) #8

hi @sivic please :slight_smile: you’re welcome.

as of collision detection and physics you’ll need access to the hype beta 4.0.

please contact @jonathan or @Daniel for Betaaccess .
I’ll provide the hypefile including sceneswitch for collision and offroad in the betasection …


(jamal) #9

Thanks a lot Hans I appreciate your support


#10

Isn’t that a commonly held dream of the community? :smile:


(Rick) #11

Wouldn’t that be great? :kissing_heart:


(jamal) #12


Thank you very much

@Daniel
@h_classen


(Hans-Gerd Claßen) #13

thx for sharing. really neat! :slight_smile:

btw: it seems to me that the collisiondetection works not (<-first race) or slow (<-second race). i could not experience this behaviour in my sourcefile. so it’ll be worth having a look, because the ability to check for the physicsbodies collisions relies to the betaversion of hype …
if it’s not caused by a scriptingissue, hype should have a look … :slight_smile: